我的Jxl使用总结
2016-03-17 16:28
260 查看
创建或读取一个工作薄
创建一个工作薄:
// 创建Excel工作表 指定名称和位置
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);
WritableCellFormat center = new WritableCellFormat();
center.setAlignment(Alignment.CENTRE); //字体在单元格居中
sheet.addCell(new Label(2, 2, "POS折扣批次号(BatchID):",center));
//new Label(a, b, 单元格显示文本,center) a,b从0开始,a为列,b为行
sheet.mergeCells(a, b, c, d);//(a,b)为起始单元格的列和行,(c,d)为结束单元格的列和行
wb.write();
wb.close();
os.close();
// 下载excel文件
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.File;
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.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jxl.Cell;
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);
以下内容转载:
API总结
1、创建或读取一个工作薄 Workbook
创建一个工作薄,就是整个Excel文档,
WritableWorkbook wwb = Workbook.createWorkbook(os);
其中os为一个文件输出流。当然还有很多其他的入参,比如File等。
Workbook不但能用来创建工作薄,也可以读取现有的工作薄,比如:
Workbook.getWorkbook(java.io.File file);
Workbook是一个很重要工具类,里面方法基本上都是static的,使用方便。
2、创建工作表 Sheet
创建工作表的方式是通过上面创建的WritableWorkbook对象来操作。
创建一个工作表:
createSheet(java.lang.String name, int index),
两个参数分别是工作表名字和插入位置,这个位置从0开始,比如:
WritableSheet sheet = wwb.createSheet("演员表", 0);
3、创建标签 Label
实际上标签这里的意思就是工作表的单元格,这个单元格多种,分别对应不同的类,比如jxl.write.Boolean、jxl.write.Boolean等。
Label label = new Label(col, row, title);
三个参数分别表示col+1列,row+1行,标题内容是title。
将标签加入到工作表中
sheet.addCell(label);
4、填充数据
数据填充这块稍微复杂点,涉及到数据单元格的格式问题。
a)、填充数字
jxl.write.Number numb = new jxl.write.Number(1, 1, 250);
sheet.addCell(numb);
b)、填充格式化的数字
jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#.##");
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);
c)、填充日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String newdate = sdf.format(new Date());
label = new Label(2, 2, newdate);
sheet.addCell(label);
d)、填充文本
label = new Label(3, 3, "周星驰");
sheet.addCell(label);
e)、填充boolean值
jxl.write.Boolean bool = new jxl.write.Boolean(4, 1, true);
sheet.addCell(bool);
5、合并单元格
通过writablesheet.mergeCells(int x,int y,int m,int n);来实现的。
表示将从第x+1列,y+1行到m+1列,n+1行合并 (四个点定义了两个坐标,左上角和右下角)
结果是合并了m-x+1行,n-y+1列,两者乘积就是合并的单元格数量。
sheet.mergeCells(0, 6, 3, 8);
label = new Label(0, 6, "合并了12个单元格");
sheet.addCell(label);
6、添加单元格的式样
主要是改变单元格背景、字体、颜色等等。
WritableCellFormat wc = new WritableCellFormat();
// 设置居中
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);
7、设置单元格字体
// 设置字体
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);
8、将工作写成文件
// 写入数据
wwb.write();
// 关闭文件
wwb.close();
9、行列的批量操作
//获取所有的工作表
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);
10、获取单元格的值
//获取单元格的值,不管什么单元格,返回都是字符串
String value = c.getContents();
WritableWorkbook workbook = Workbook.createWorkbook(tempFile);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
WritableSheet sheet = workbook.createSheet("TestCreateExcel", 0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//一些临时变量,用于写到excel中
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Label l=null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
jxl.write.Number n=null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
jxl.write.DateTime d=null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//预定义的一些字体和格式,同一个Excel中最好不要有太多格式
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
WritableFont headerFont = new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
false, Underlinestyle.NO_UNDERLINE, jxl.format.Colour.BLUE);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
WritableCellFormat headerFormat = new WritableCellFormat (headerFont);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
WritableFont titleFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
false, Underlinestyle.NO_UNDERLINE, jxl.format.Colour.RED);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
WritableCellFormat titleFormat = new WritableCellFormat (titleFont);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
WritableFont detFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
false, Underlinestyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
WritableCellFormat detFormat = new WritableCellFormat (detFont);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
NumberFormat nf=new NumberFormat("0.00000"); //用于Number的格式
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
WritableCellFormat priceFormat = new WritableCellFormat (detFont, nf);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
DateFormat df=new DateFormat("yyyy-MM-dd");//用于日期的
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
WritableCellFormat dateFormat = new WritableCellFormat (detFont, df);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//剩下的事情,就是用上面的内容和格式创建一些单元格,再加到sheet中
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
l=new Label(0, 0, "用于测试的Excel文件", headerFormat);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.addCell(l);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//add Title
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int column=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
l=new Label(column++, 2, "标题", titleFormat);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.addCell(l);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
l=new Label(column++, 2, "日期", titleFormat);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.addCell(l);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
l=new Label(column++, 2, "货币", titleFormat);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.addCell(l);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
l=new Label(column++, 2, "价格", titleFormat);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.addCell(l);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//add detail
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int i=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
column=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
l=new Label(column++, i+3, "标题 "+i, detFormat);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.addCell(l);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
d=new DateTime(column++, i+3, new java.util.Date(), dateFormat);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.addCell(d);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
l=new Label(column++, i+3, "CNY", detFormat);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.addCell(l);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
n=new jxl.write.Number(column++, i+3, 5.678, priceFormat);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.addCell(n);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
i++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
column=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
l=new Label(column++, i+3, "标题 "+i, detFormat);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.addCell(l);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
d=new DateTime(column++, i+3, new java.util.Date(), dateFormat);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.addCell(d);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
l=new Label(column++, i+3, "SGD", detFormat);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.addCell(l);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
n=new jxl.write.Number(column++, i+3, 98832, priceFormat);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.addCell(n);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//设置列的宽度
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
column=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.setColumnView(column++, 20);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.setColumnView(column++, 20);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.setColumnView(column++, 10);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.setColumnView(column++, 20);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
workbook.write();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
workbook.close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
我正在做的项目里定义了很多样式,好像速度也不算慢,还有一些其他方面的定义,补充到下面:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//构造格式:ARIAL字体、10号、粗体、非斜体、无下划线、黑色
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
WritableFont fmtx2TotalCaption = new WritableFont(WritableFont.ARIAL,10,WritableFont.BOLD,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
WritableCellFormat totalx2Format = new WritableCellFormat(fmtx2TotalCaption);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//文字垂直居中对齐
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
totalx2Format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//文字水平居中对齐
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
totalx2Format.setAlignment(jxl.format.Alignment.CENTRE);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//边框深蓝色
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
totalx2Format.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
jxl.format.Colour.DARK_BLUE);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//设置底色为冰蓝
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
totalx2Format.setBackground(jxl.format.Colour.ICE_BLUE);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.mergeCells(0, row, 8, row); //合并单元格,row 到 row 行,0 到 8 列
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.setRowView(row, 600);// 第 row 行的高度
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
另外就是要在 Action(servlet) 中这么写,IE就会直接弹出保存文件的对话框(注意要占用当前窗口):
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
String title = "XXXX统计";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
OutputStream out = response.getOutputStream();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
WritableWorkbook wb = Workbook.createWorkbook(out);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
response.setContentType("aplication/vnd.ms-excel");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
response.addHeader("Content-Disposition","inline; filename=" + new String(title.getBytes("GB2312"),"ISO8859_1") + ".xls"); //有中文必须转码
创建一个工作薄:
// 创建Excel工作表 指定名称和位置
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);
WritableCellFormat center = new WritableCellFormat();
center.setAlignment(Alignment.CENTRE); //字体在单元格居中
sheet.addCell(new Label(2, 2, "POS折扣批次号(BatchID):",center));
//new Label(a, b, 单元格显示文本,center) a,b从0开始,a为列,b为行
sheet.mergeCells(a, b, c, d);//(a,b)为起始单元格的列和行,(c,d)为结束单元格的列和行
wb.write();
wb.close();
os.close();
// 下载excel文件
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.File;
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.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jxl.Cell;
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);
以下内容转载:
API总结
1、创建或读取一个工作薄 Workbook
创建一个工作薄,就是整个Excel文档,
WritableWorkbook wwb = Workbook.createWorkbook(os);
其中os为一个文件输出流。当然还有很多其他的入参,比如File等。
Workbook不但能用来创建工作薄,也可以读取现有的工作薄,比如:
Workbook.getWorkbook(java.io.File file);
Workbook是一个很重要工具类,里面方法基本上都是static的,使用方便。
2、创建工作表 Sheet
创建工作表的方式是通过上面创建的WritableWorkbook对象来操作。
创建一个工作表:
createSheet(java.lang.String name, int index),
两个参数分别是工作表名字和插入位置,这个位置从0开始,比如:
WritableSheet sheet = wwb.createSheet("演员表", 0);
3、创建标签 Label
实际上标签这里的意思就是工作表的单元格,这个单元格多种,分别对应不同的类,比如jxl.write.Boolean、jxl.write.Boolean等。
Label label = new Label(col, row, title);
三个参数分别表示col+1列,row+1行,标题内容是title。
将标签加入到工作表中
sheet.addCell(label);
4、填充数据
数据填充这块稍微复杂点,涉及到数据单元格的格式问题。
a)、填充数字
jxl.write.Number numb = new jxl.write.Number(1, 1, 250);
sheet.addCell(numb);
b)、填充格式化的数字
jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#.##");
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);
c)、填充日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String newdate = sdf.format(new Date());
label = new Label(2, 2, newdate);
sheet.addCell(label);
d)、填充文本
label = new Label(3, 3, "周星驰");
sheet.addCell(label);
e)、填充boolean值
jxl.write.Boolean bool = new jxl.write.Boolean(4, 1, true);
sheet.addCell(bool);
5、合并单元格
通过writablesheet.mergeCells(int x,int y,int m,int n);来实现的。
表示将从第x+1列,y+1行到m+1列,n+1行合并 (四个点定义了两个坐标,左上角和右下角)
结果是合并了m-x+1行,n-y+1列,两者乘积就是合并的单元格数量。
sheet.mergeCells(0, 6, 3, 8);
label = new Label(0, 6, "合并了12个单元格");
sheet.addCell(label);
6、添加单元格的式样
主要是改变单元格背景、字体、颜色等等。
WritableCellFormat wc = new WritableCellFormat();
// 设置居中
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);
7、设置单元格字体
// 设置字体
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);
8、将工作写成文件
// 写入数据
wwb.write();
// 关闭文件
wwb.close();
9、行列的批量操作
//获取所有的工作表
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);
10、获取单元格的值
//获取单元格的值,不管什么单元格,返回都是字符串
String value = c.getContents();
WritableWorkbook workbook = Workbook.createWorkbook(tempFile);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
WritableSheet sheet = workbook.createSheet("TestCreateExcel", 0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//一些临时变量,用于写到excel中
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Label l=null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
jxl.write.Number n=null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
jxl.write.DateTime d=null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//预定义的一些字体和格式,同一个Excel中最好不要有太多格式
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
WritableFont headerFont = new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
false, Underlinestyle.NO_UNDERLINE, jxl.format.Colour.BLUE);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
WritableCellFormat headerFormat = new WritableCellFormat (headerFont);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
WritableFont titleFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
false, Underlinestyle.NO_UNDERLINE, jxl.format.Colour.RED);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
WritableCellFormat titleFormat = new WritableCellFormat (titleFont);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
WritableFont detFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
false, Underlinestyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
WritableCellFormat detFormat = new WritableCellFormat (detFont);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
NumberFormat nf=new NumberFormat("0.00000"); //用于Number的格式
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
WritableCellFormat priceFormat = new WritableCellFormat (detFont, nf);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
DateFormat df=new DateFormat("yyyy-MM-dd");//用于日期的
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
WritableCellFormat dateFormat = new WritableCellFormat (detFont, df);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//剩下的事情,就是用上面的内容和格式创建一些单元格,再加到sheet中
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
l=new Label(0, 0, "用于测试的Excel文件", headerFormat);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.addCell(l);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//add Title
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int column=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
l=new Label(column++, 2, "标题", titleFormat);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.addCell(l);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
l=new Label(column++, 2, "日期", titleFormat);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.addCell(l);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
l=new Label(column++, 2, "货币", titleFormat);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.addCell(l);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
l=new Label(column++, 2, "价格", titleFormat);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.addCell(l);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//add detail
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int i=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
column=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
l=new Label(column++, i+3, "标题 "+i, detFormat);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.addCell(l);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
d=new DateTime(column++, i+3, new java.util.Date(), dateFormat);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.addCell(d);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
l=new Label(column++, i+3, "CNY", detFormat);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.addCell(l);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
n=new jxl.write.Number(column++, i+3, 5.678, priceFormat);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.addCell(n);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
i++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
column=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
l=new Label(column++, i+3, "标题 "+i, detFormat);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.addCell(l);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
d=new DateTime(column++, i+3, new java.util.Date(), dateFormat);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.addCell(d);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
l=new Label(column++, i+3, "SGD", detFormat);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.addCell(l);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
n=new jxl.write.Number(column++, i+3, 98832, priceFormat);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.addCell(n);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//设置列的宽度
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
column=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.setColumnView(column++, 20);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.setColumnView(column++, 20);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.setColumnView(column++, 10);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.setColumnView(column++, 20);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
workbook.write();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
workbook.close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
我正在做的项目里定义了很多样式,好像速度也不算慢,还有一些其他方面的定义,补充到下面:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//构造格式:ARIAL字体、10号、粗体、非斜体、无下划线、黑色
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
WritableFont fmtx2TotalCaption = new WritableFont(WritableFont.ARIAL,10,WritableFont.BOLD,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
WritableCellFormat totalx2Format = new WritableCellFormat(fmtx2TotalCaption);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//文字垂直居中对齐
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
totalx2Format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//文字水平居中对齐
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
totalx2Format.setAlignment(jxl.format.Alignment.CENTRE);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//边框深蓝色
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
totalx2Format.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
jxl.format.Colour.DARK_BLUE);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//设置底色为冰蓝
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
totalx2Format.setBackground(jxl.format.Colour.ICE_BLUE);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.mergeCells(0, row, 8, row); //合并单元格,row 到 row 行,0 到 8 列
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sheet.setRowView(row, 600);// 第 row 行的高度
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
另外就是要在 Action(servlet) 中这么写,IE就会直接弹出保存文件的对话框(注意要占用当前窗口):
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
String title = "XXXX统计";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
OutputStream out = response.getOutputStream();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
WritableWorkbook wb = Workbook.createWorkbook(out);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
response.setContentType("aplication/vnd.ms-excel");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
response.addHeader("Content-Disposition","inline; filename=" + new String(title.getBytes("GB2312"),"ISO8859_1") + ".xls"); //有中文必须转码
相关文章推荐
- CentOS6.3重新加载网卡报错 Active connection path: /org/freedesktop/NetworkManager/ActiveConnection
- git 解决fatal: Not a git repository
- C++ 之 新式转型操作符
- 复利软件单利及期望值的实现
- 关于mtk Android打开串口权限问题
- Android悬浮窗进阶版-中篇(代码篇)
- poj2299(转)
- CF 596A. Wilbur and Swimming Pool 【计算矩形面积】
- Double click 转成power之后的上报
- poj1328 radar installation
- Java循环基础
- do { x } while (__LINE__ == -1)
- 计算前50个素数
- [leetcode] @python 115. Distinct Subsequences
- .net4.0调用非托管DLL的异常捕获
- sed 递归替换
- 学习英语
- 【编程工具】MyEclipse中的常用快捷键
- spring mvc3 静态文件放在WEB-INF下无法访问解决和解决@Controller失效问题
- python 刷题:实现队列