String fileName = File.separator +"POS全场折扣-批次:"+batchid; //文件名
// createExcel文件
String filePath = FileUtil.POS_root + File.separator +"全场POS折扣信息-批次号:"+batchid;//路径
if (!new File(filePath).exists()) {
new File(filePath).mkdirs();
}
filePath += fileName + ".xls";
OutputStream os = new FileOutputStream(filePath);
WritableWorkbook wb = Workbook.createWorkbook(os);
// 创建Excel工作表 指定sheet的名称和位置
WritableSheet sheet = wb.createSheet("POS折扣信息", 0);
center.setAlignment(Alignment.CENTRE); //字体在单元格居中
sheet.addCell(new Label(2, 2, "POS折扣批次号(BatchID):",center));
sheet.mergeCells(a, b, c, d);//(a,b)为起始单元格的列和行,(c,d)为结束单元格的列和行
wb.close();
os.close();
OutputStream os2 = response.getOutputStream();
try {
File f = new File(filePath);
response.setContentType("application/vnd.ms-excel; charset=GB2312");// 文件内容乱码
response.setHeader("Content-Disposition", "attachment;filename="
+ URLEncoder.encode(f.getName(), "UTF-8"));
byte buffer[] = new byte[4096];
FileInputStream fin = new FileInputStream(f);
int size;
while ((size = fin.read(buffer)) != -1) {
os2.write(buffer, 0, size);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
os2.close();
}
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletResponse;
import jxl.CellView;
import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.Alignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
//构造格式:ARIAL字体、10号、粗体、非斜体、无下划线、黑色
WritableFont header = new WritableFont(WritableFont.createFont("微软雅黑"),10,WritableFont.BOLD,
false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK);
WritableCellFormat heaerline = new WritableCellFormat(header);
//文字垂直居中对齐
heaerline.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
//文字水平居中对齐
heaerline.setAlignment(jxl.format.Alignment.CENTRE);
//内容格式
sheet.setColumnView(1, 20);;// 第 第二列 的宽度20
WritableCellFormat center = new WritableCellFormat();
center.setAlignment(Alignment.CENTRE);
sheet.addCell(new Label(2, 2, "POS折扣批次号(BatchID):",heaerline));
sheet.mergeCells(2, 2, 5, 2);
sheet.addCell(numb);
jxl.write.WritableCellFormat wcf = new jxl.write.WritableCellFormat(nf);
jxl.write.Number n = new jxl.write.Number(2, 1, 2.451, wcf);
sheet.addCell(n);
String newdate = sdf.format( new Date());
label = new Label(2, 2, newdate);
sheet.addCell(label);
sheet.addCell(label);
sheet.addCell(bool);
label = new Label(0, 6, "合并了12个单元格");
sheet.addCell(label);
// 设置居中
wc.setAlignment(Alignment.CENTRE);
// 设置边框线
wc.setBorder(Border.ALL, BorderLineStyle.THIN);
// 设置单元格的背景颜色
wc.setBackground(jxl.format.Colour.RED);
label = new Label(1, 5, "字体", wc);
sheet.addCell(label);
jxl.write.WritableFont wfont = new jxl.write.WritableFont(WritableFont.createFont( "楷书"), 20);
WritableCellFormat font = new WritableCellFormat(wfont);
label = new Label(2, 6, "楷书", font);
sheet.addCell(label);
wwb.write();
// 关闭文件
wwb.close();
jxl.write.WritableSheet[] sheetList = wwb.getSheets();
//获取第1列所有的单元格
jxl.Cell[] cellc = sheet.getColumn(0);
//获取第1行所有的单元格
jxl.Cell[] cellr = sheet.getRow(0);
//获取第1行第1列的单元格
Cell c = sheet.getCell(0, 0);
String value = c.getContents();
WritableWorkbook workbook = Workbook.createWorkbook(tempFile);
WritableSheet sheet = workbook.createSheet("TestCreateExcel", 0);
//一些临时变量,用于写到excel中
Label l=null;
jxl.write.Number n=null;
jxl.write.DateTime d=null;
//预定义的一些字体和格式,同一个Excel中最好不要有太多格式
WritableFont headerFont = new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD,
false, Underlinestyle.NO_UNDERLINE, jxl.format.Colour.BLUE);
WritableCellFormat headerFormat = new WritableCellFormat (headerFont);
WritableFont titleFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD,
false, Underlinestyle.NO_UNDERLINE, jxl.format.Colour.RED);
WritableCellFormat titleFormat = new WritableCellFormat (titleFont);
WritableFont detFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD,
false, Underlinestyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
WritableCellFormat detFormat = new WritableCellFormat (detFont);
NumberFormat nf=new NumberFormat("0.00000"); //用于Number的格式
WritableCellFormat priceFormat = new WritableCellFormat (detFont, nf);
DateFormat df=new DateFormat("yyyy-MM-dd");//用于日期的
WritableCellFormat dateFormat = new WritableCellFormat (detFont, df);
//剩下的事情,就是用上面的内容和格式创建一些单元格,再加到sheet中
l=new Label(0, 0, "用于测试的Excel文件", headerFormat);
sheet.addCell(l);
//add Title
int column=0;
l=new Label(column++, 2, "标题", titleFormat);
sheet.addCell(l);
l=new Label(column++, 2, "日期", titleFormat);
sheet.addCell(l);
l=new Label(column++, 2, "货币", titleFormat);
sheet.addCell(l);
l=new Label(column++, 2, "价格", titleFormat);
sheet.addCell(l);
//add detail
int i=0;
column=0;
l=new Label(column++, i+3, "标题 "+i, detFormat);
sheet.addCell(l);
d=new DateTime(column++, i+3, new java.util.Date(), dateFormat);
sheet.addCell(d);
l=new Label(column++, i+3, "CNY", detFormat);
sheet.addCell(l);
n=new jxl.write.Number(column++, i+3, 5.678, priceFormat);
sheet.addCell(n);
i++;
column=0;
l=new Label(column++, i+3, "标题 "+i, detFormat);
sheet.addCell(l);
d=new DateTime(column++, i+3, new java.util.Date(), dateFormat);
sheet.addCell(d);
l=new Label(column++, i+3, "SGD", detFormat);
sheet.addCell(l);
n=new jxl.write.Number(column++, i+3, 98832, priceFormat);
sheet.addCell(n);
//设置列的宽度
column=0;
sheet.setColumnView(column++, 20);
sheet.setColumnView(column++, 20);
sheet.setColumnView(column++, 10);
sheet.setColumnView(column++, 20);
workbook.write();
workbook.close();
我正在做的项目里定义了很多样式,好像速度也不算慢,还有一些其他方面的定义,补充到下面:
WritableFont fmtx2TotalCaption = new WritableFont(WritableFont.ARIAL, 10 ,WritableFont.BOLD,
false ,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK);
WritableCellFormat totalx2Format = new WritableCellFormat(fmtx2TotalCaption);
// 文字垂直居中对齐
totalx2Format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
// 文字水平居中对齐
totalx2Format.setAlignment(jxl.format.Alignment.CENTRE);
// 边框深蓝色
totalx2Format.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN,
jxl.format.Colour.DARK_BLUE);
// 设置底色为冰蓝
totalx2Format.setBackground(jxl.format.Colour.ICE_BLUE);
sheet.mergeCells( 0 , row, 8 , row); // 合并单元格,row 到 row 行,0 到 8 列
sheet.setRowView(row, 600 ); // 第 row 行的高度
另外就是要在 Action(servlet) 中这么写,IE就会直接弹出保存文件的对话框(注意要占用当前窗口):
OutputStream out = response.getOutputStream();
WritableWorkbook wb = Workbook.createWorkbook(out);
response.setContentType( " aplication/vnd.ms-excel " );
response.addHeader( " Content-Disposition " , " inline; filename= " + new String(title.getBytes( " GB2312 " ), " ISO8859_1 " ) + " .xls " ); // 有中文必须转码