Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java操作Excel #3

Open
muyuqiu001 opened this issue Sep 4, 2017 · 2 comments
Open

Java操作Excel #3

muyuqiu001 opened this issue Sep 4, 2017 · 2 comments

Comments

@muyuqiu001
Copy link
Owner

No description provided.

@muyuqiu001
Copy link
Owner Author

读取:

2 Jxl开发指南
2.1 介绍
jxl操作excel包括对象Workbook,Sheet ,Cell。
一个excel就对应一个Workbook对象,
一个Workbook可以有多个Sheet对象
一个Sheet对象可以有多个Cell对象
2.2 读取excel操作
通过Workbook,Sheet ,Cell这三个对象我们就可以实现Excel文件的读取工作。我们先想想一下读取步骤,不管是什么样的Excel操作框架必定都要经历
1、 选取Excel文件得到工作薄
2、 选择工作表
3、 选择Cell
4、 读取信息
2.2.1 读取工作薄
选取Excel文件得到工作薄Workbook
Workbook workbook = Workbook.getWorkbook(new File("myfile.xls"));
2.2.2 读取工作表
通过Workbook的getSheet方法选择第一个工作表(从0开始)
Sheet sheet = workbook.getSheet(0);
也可以通过工作的名称来得到Sheet
2.2.3 读取单元格
通过Sheet的getCell方法选择位置为C2的单元格(两个参数都从0开始)
Cell c2 = sheet.getCell(2,1);

2.2.3.1 读取单元格的值
2.2.3.2 通过Cell的getContents方法
把单元格中的信息以字符的形式读取出来String stringc2 = c2.getContents();
2.2.3.3 Cell提供了一个getType方法
能够返回单元格的类型信息,同时JXL提供了一个CellType类用来预设Excel中的类型信息,而且JXL提供了一些Cell类的子类用来分别用来表示各种类型的单元格,如LabelCell,NumberCell,DateCell分别表示字符、数值、日期类型的单元格
if (c2.getType() == CellType. LABEL)
{
LabelCell nc = (LabelCell) c2;
String number b2 = nc. getString();
}
if (c2.getType() == CellType. DATE)
{
DateCell nc = (DateCell) c2;
Date number b2 = nc. getDate();
}

if (c2.getType() == CellType.NUMBER)
{
NumberCell nc = (NumberCell) c2;
double number b2 = nc.getValue();
}
API提供了以下基本类型,与Excel的数据格式相对应,如下图所示

2.2.4 以释放资源:workbook.close()
当你完成对Excel电子表格数据的处理后,一定要使用close()方法来关闭先前创建的对象,以释放读取数据表的过程中所占用的内存空间,在读取大量数据时显得尤为重要
最后不要忘记关闭workbook以释放资源:workbook.close();

@muyuqiu001
Copy link
Owner Author

muyuqiu001 commented Sep 4, 2017

写入:
通过WritableWorkbook,WritableSheet,Label这三个对象我们就可以实现Excel文件的插入工作。我们先想想一下插入,不管是什么样的Excel操作框架必定都要经历
1、 创建Exce工作薄
2、 创建工作表
3、 创建单元格

2.3.1 创建工作薄
API提供了两种方式来处理可写入的输出流,一种是直接生成本地文件,如果文件名不带全路径的话,缺省的文件会定位在当前目录,如果文件名带有全路径的话,则生成的Excel文件则会定位在相应的目录;另外一种是将Excel对象直接写入到输出流,例如:用户通过浏览器来访问web服务器,如果HTTP头设置正确的话,浏览器自动调用客户端的Excel应用程序,来显示动态生成的Excel电子表格。
2.3.1.1 创建可写入的Excel工作薄
WritableWorkbook
wwb = Workbook.createWorkbook(new File(targetfile));
2.3.1.2 将WritableWorkbook直接写入到输出流
OutputStream os = new FileOutputStream(targetfile);
WritableWorkbook wwb = Workbook.createWorkbook(os);
2.3.2 创建工作表
WritableSheet ws = wwb.createSheet("通讯录", 0);//创建sheet
2.3.3 创建单元格
2.3.3.1 添加文本类单元格
Label labelC = new Label(0, 0, "This is a Label cell");
2.3.3.2 添加带有字型Formatting的对象
WritableFont wf = new WritableFont(WritableFont.TIMES, 18, WritableFont.BOLD, true);
WritableCellFormat wcfF = new WritableCellFormat(wf);
labelCF = new Label(1, 0, "This is a Label Cell", wcfF);
ws.addCell(labelCF);
2.3.3.3 添加带有字体颜色Formatting的对象
WritableFont wfc = new WritableFont(WritableFont.ARIAL,10,WritableFont.NO_BOLD, false,
UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED);
WritableCellFormat wcfFC = new WritableCellFormat(wfc);
Label labelCFC = new Label(1, 0, "This is a Label Cell", wcfFC);
ws.addCell(labelCF);
2.3.3.4 添加Number对象
Number labelN = new jxl.write.Number(0, 1, 3.1415926);
ws.addCell(labelN);
2.3.3.5 添加带有formatting的Number对象
NumberFormat nf = new NumberFormat("#.##");
WritableCellFormat wcfN = new WritableCellFormat(nf);
Number labelNF = new Number(1, 1, 3.1415926, wcfN);
ws.addCell(labelNF);
2.3.3.6 添加Boolean对象
Boolean labelB = new jxl.write.Boolean(0, 2, false);
ws.addCell(labelB);
2.3.3.7 添加DateTime对象
DateTime labelDT = new DateTime(0, 3, new java.util.Date());
ws.addCell(labelDT);
2.3.3.8 添加带有formatting的DateFormat对象
DateFormat df = new DateFormat("dd MM yyyy hh:mm:ss");
WritableCellFormat wcfDF = new WritableCellFormat(df);
DateTime labelDTF = new DateTime(1, 3, new Date(), wcfDF);
ws.addCell(labelDTF);
2.3.3.9 添加公式单元格
Fornual formual = new Formual(0,11,”Sum(A1:A9)”);
wrb.addCell(formual);
2.3.3.10 添加图像
WritableImage wrimage=new WritableImage(1,5,10,10,new File(imageFilepath));
wrb.addImage(wrimage);
注意,API中注明只支持png文件。

2.3.4 合并单元格
通过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);

2.3.5 添加单元格样式
主要是改变单元格背景、字体、颜色等等。
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);
2.3.6 设置单元格字体
WritableFont wfont =
new WritableFont(WritableFont.createFont("楷书"), 20);
WritableCellFormat font = new WritableCellFormat(wfont);
label = new Label(2, 6, "楷书", font);
sheet.addCell(label);
2.3.7 写入到文件
wwb.write();// 写入数据
wwb.close();// 关闭文件

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant