File tempFile=new File("d:/temp/output.xls");
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();
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();
我正在做的项目里定义了很多样式,好像速度也不算慢,还有一些其他方面的定义,补充到下面:
//构造格式:ARIAL字体、10号、粗体、非斜体、无下划线、黑色
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 行的高度
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就会直接弹出保存文件的对话框(注意要占用当前窗口):
String title = "XXXX统计";
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"); //有中文必须转码
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"); //有中文必须转码
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1526490
相关推荐
jxl导出数据到Excel,字符串转成图片,日期类
通过给出URL,用JXL进行下载 可以直接存到指定的目录
jxl操作excel实例,jxl jar包下载 jxl操作excel实例,jxl jar包下载 jxl操作excel实例,jxl jar包下载
java中使用jxl打印excel报表,java中使用jxl打印excel报表,java中使用jxl打印excel报表
java jxl Excel 读写 合并单元格 下载 等实例
jxl导出excel加水印 WritableWorkbook、WritableSheet、WritableCellFormat
jxl导出excel 完整例子工程jxl导出excel 完整例子工程jxl导出excel 完整例子工程 jxl导出excel 完整例子工程jxl导出excel 完整例子工程jxl导出excel 完整例子工程
使用Jxl对Excel进行导入导出,其中包含设置单元格样式以及数据有效性。
JXL 实现Excel的导入及开发包 JXL 实现Excel的导入及开发包 JXL 实现Excel的导入及开发包
jxl的excel读写操作 java操作excel的读写。
使用jxl操作Excel,可以作为一个帮助文件
jxl对excel添加水印(含有setWaterMarkImage方法).zip
学习使用JXL读写EXCEL文件, 学习使用JXL读写EXCEL文件。
利用jxl操作excel文件,提取exce文件数据生成txt文件。
jxl实现excel上传下载全部功能, 详细代码和java文件
NULL 博文链接:https://chaoyi.iteye.com/blog/2174163
jxl操作excel模板jar包,用于导出特定excel模板
使用jxl可以快速进行excel文档的建立,读写和建立Sheet页等操作.
jxl excel java demo 整理后
jxl JAVA Excel解析,主要用于修改、上传、下载Excel文件。