最近在公司的项目里发现了对excel表格操作的java代码,这里所说的操作,无外乎就是增删改查,下面简略的归纳下jxl相关知识要点。
jxl,是一种通过java来操作excel的技术,其API是纯java的,不依赖于操作系统,无论是Windows,还是Linux,都能够正确的处理Excel文件。支持Excel 95-2000的所有版本,生成Excel 2000标准格式,支持字体、数字、日期操作,能够修饰单元格属性,支持图像和图表,但是这里需要指出的是,jxl所支持的图像格式非常有限,仅仅是PNG
格式。
jxl能够满足一般简单报表的基本需求,如果想操作更为复杂的excel,小编建议使用apache的POI,POI是一种比jxl更为强大的对excel做操作的技术,这里着重讲解jxl,毕竟这两种技术在原理上都大同小异,并且jxl产生于POI之前,所以我们有必要先搞清楚jxl的使用,再逐步跟进POI的学习。
首先,我们来了解一下excel的组成,excel工作软件我们再熟悉不过了,它的组成从大到小主要是工作簿,页和单元格,一个excel文件就是一个工作簿,里面可以包含多页,每一页的组成元素是每一个小小的单元格,而我们要学习的操作主要就是针对页和单元格。
1.新增一个excel文件
package com;
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class SimpleExcelWrite {
public void createExcel(OutputStream os) throws IOException, RowsExceededException, WriteException{
//创建工作簿
WritableWorkbook workbook = Workbook.createWorkbook(os);
//创建新的一页
WritableSheet sheet = workbook.createSheet("first sheet", 0);
sheet.setName("fuqinqin");
//创建要显示的内容,创建一个单元格,第一个参数为列坐标,第二个参数为行坐标,第三个参数内容
Label xuexiao = new Label(0,0,"学校");
sheet.addCell(xuexiao);
Label zhuanye = new Label(1,0,"专业");
sheet.addCell(zhuanye);
Label jingzhengli = new Label(2,0,"专业竞争力");
sheet.addCell(jingzhengli);
Label qinghua = new Label(0,1,"清华大学");
sheet.addCell(qinghua);
Label jisuanji = new Label(1,1,"计算机专业");
sheet.addCell(jisuanji);
Label gao = new Label(2,1,"高");
sheet.addCell(gao);
Label beida = new Label(0,2,"北京大学");
sheet.addCell(beida);
Label falv = new Label(1,2,"法律专业");
sheet.addCell(falv);
Label zhong = new Label(2,2,"中");
sheet.addCell(zhong);
Label beihang = new Label(0,3,"北京航空航天大学");
sheet.addCell(beihang);
Label guangdian = new Label(1,3,"光电专业");
sheet.addCell(guangdian);
Label di = new Label(2,3,"低");
sheet.addCell(di);
//合并单元格 arg1起始列角标 arg2起始行角标 ar3终止列角标 arg4终止行角标
sheet.mergeCells(0, 4, 2, 4);
Label hebing = new Label(0,4,"合并测试");
WritableCellFormat wcf = new WritableCellFormat();
wcf.setAlignment(Alignment.CENTRE);
//垂直居中
wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
//是否换行
wcf.setWrap(true);
hebing.setCellFormat(wcf);
sheet.addCell(hebing);
//设置冻结单元格,如下是设置第一行为冻结行,上下拉动滚动条始终显示
sheet.getSettings().setVerticalFreeze(1);
//设置某行的行高度
sheet.setRowView(4, 600);
//设置某列的宽度
sheet.setColumnView(0, 20);
//将创建的北荣写入到输入流中,并且关闭输出流
workbook.write();
workbook.close();
os.close();
}
public static void main(String[] args) throws RowsExceededException, WriteException, IOException {
File file = new File("D://test.xls");
OutputStream os = new FileOutputStream(file);
SimpleExcelWrite sew = new SimpleExcelWrite();
sew.createExcel(os);
System.out.println("--SUCCESS--");
}
2.查看一个excel文件
package com;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.WritableCell;
public class QueryExcelWrite {
public static void main(String[] args) throws BiffException, IOException {
File file = new File("D://test.xls");
InputStream in = new FileInputStream(file);
Workbook workbook = Workbook.getWorkbook(file);
Sheet[] sheets = workbook.getSheets();
if(sheets == null){
return;
}
for(int i=0;i<sheets.length;i++){
Sheet sheet = sheets[i];
int rows = sheet.getRows();
int cols = sheet.getColumns();
System.out.println("sheetName = "+sheet.getName());
System.out.println("rows = "+rows);
System.out.println("cols = "+cols);
for(int j=0;j<rows;j++){
for(int k=0;k<cols;k++){
Cell cell = (Cell) sheet.getCell(k, j);
System.out.print(cell.getContents()+"\t");
}
System.out.println();
}
System.out.println("------------------------------------");
}
}
}
3.修改excel文件 —— 修改就是在查看的基础上,拿到每个单元格或者页,然后做一些set操作
4,。jxl里面的一些数据类型
package com;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import jxl.CellReferenceHelper;
import jxl.Workbook;
import jxl.write.DateFormats;
import jxl.write.DateTime;
import jxl.write.Formula;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class DataTypeExcelWrite {
public void createExcel(OutputStream os) throws IOException, RowsExceededException, WriteException{
//床架工作簿
WritableWorkbook workbook = Workbook.createWorkbook(os);
//创建新的一页
WritableSheet sheet = workbook.createSheet("first sheet", 0);
//创建内容
Label format = new Label(0,0,"数据格式");
sheet.addCell(format);
Label floats = new Label(1,0,"浮点型");
sheet.addCell(floats);
Label floats_2 = new Label(6,0,"浮点型2");
sheet.addCell(floats_2);
Label integer = new Label(2,0,"整形");
sheet.addCell(integer);
Label booleans = new Label(3,0,"布尔型");
sheet.addCell(booleans);
Label date = new Label(4,0,"日期类型");
sheet.addCell(date);
Label formula = new Label(5,0,"formula");
sheet.addCell(formula);
Label example = new Label(0,1,"数据示例");
sheet.addCell(example);
//浮点数据
Number number = new Number(1,1,3);
sheet.addCell(number);
//浮点数据
Number number2 = new Number(6,1,7);
sheet.addCell(number2);
//整形数据
Number ints = new Number(2,1,111);
sheet.addCell(ints);
//布尔型
// Boolean bools = new Boolean(3,1,true);
// sheet.addCell(bools);
Number number3 = new Number(3,1,7);
sheet.addCell(number3);
//日期类型
Date d = new Date();
WritableCellFormat f = new WritableCellFormat(DateFormats.FORMAT1);
DateTime dt = new DateTime(4,1,d,f);
sheet.addCell(dt);
//formula
Formula ff = new Formula(5,1,"SUM(B2:D2)");
sheet.addCell(ff);
//将北荣写入输出流,并且关闭流
workbook.write();
workbook.close();
os.close();
}
public static void main(String[] args) throws RowsExceededException, WriteException, FileNotFoundException, IOException {
DataTypeExcelWrite dtew = new DataTypeExcelWrite();
dtew.createExcel(new FileOutputStream(new File("D://haha.xls")));
System.out.println("--SUCCESS--");
System.out.println(CellReferenceHelper.getCellReference(11, 11));
}
}
这里面得注意Formula,可以嵌套excel公式
}