java 操作 Excel 最常用的就是JXL(java excel api)和POI,今先看下JXL吧。首先可以到 http://www.andykhan.com/jexcelapi/download.html 下载最新的jxl.jar,里面有它的jar包和API,内有一个小例子,可以参考一下。
JXL 用起来挺简单的,不过相应的其功能也并不是十分强大,对于一般的简单的excel操作还可以,对于太复杂的还是有些吃力,基本的操作也就是以下几项内容。
首先,要创建一个可读写的工作簿(WritableWorkbook):
- WritableWorkbook workbook = Workbook.createWorkbook(new File("d:\\test.xls"));
WritableWorkbook workbook = Workbook.createWorkbook(new File("d:\\test.xls"));
如果是想要修改一个已存在的excel工作簿,则需要先获得它的原始工作簿,再创建一个可读写的副本:
- Workbook wb = Workbook.getWorkbook(new File("src\\test\\test.xls"));
- WritableWorkbook workbook = Workbook.createWorkbook(new File("d:\\test_modified.xls"),wb);
Workbook wb = Workbook.getWorkbook(new File("src\\test\\test.xls")); // 获得原始文档
WritableWorkbook workbook = Workbook.createWorkbook(new File("d:\\test_modified.xls"),wb); // 创建一个可读写的副本
然后,取得我们要操作的sheet,并对其进行相应的操作,如改名、合并单元格、设置列宽、行高等:
- WritableSheet sheet = workbook.getSheet(0);
- sheet.setName("修改后");
- workbook.removeSheet(2);
- workbook.removeSheet(3);
-
- sheet.mergeCells(0, 0, 4, 0);
- sheet.setRowView(0, 600);
- sheet.setColumnView(0, 30);
- sheet.setColumnView(1, 20);
WritableSheet sheet = workbook.getSheet(0);
sheet.setName("修改后"); // 给sheet页改名
workbook.removeSheet(2); // 移除多余的标签页
workbook.removeSheet(3);
sheet.mergeCells(0, 0, 4, 0); // 合并单元格
sheet.setRowView(0, 600); // 设置行的高度
sheet.setColumnView(0, 30); // 设置列的宽度
sheet.setColumnView(1, 20); // 设置列的宽度
之后,就是对单元格的操作了,可以通过如下方法获取一个单元格,其中两个参数分别为列、行的位置,从0开始计数,如(2,3)就代表单元格C4:
- WritableCell cell = sheet.getWritableCell(2,3);
WritableCell cell = sheet.getWritableCell(2,3);
通过WritableFont、WritableCellFormat等对象可以设置单元格的字体、样式等外观:
- WritableFont titleWf = new WritableFont(WritableFont.createFont("仿宋_GB2312"),
- 20,
- WritableFont.NO_BOLD,
- false,
- UnderlineStyle.NO_UNDERLINE,
- Colour.BLUE2,
- ScriptStyle.NORMAL_SCRIPT);
- WritableCellFormat wcf = new WritableCellFormat(titleWf);
- wcf.setBackground(Colour.GRAY_25);
- wcf.setAlignment(Alignment.CENTRE);
- wcf.setBorder(Border.ALL, BorderLineStyle.THICK);
- cell.setCellFormat(wcf);
WritableFont titleWf = new WritableFont(WritableFont.createFont("仿宋_GB2312"),// 字体
20,//WritableFont.DEFAULT_POINT_SIZE, // 字号
WritableFont.NO_BOLD, // 粗体
false, // 斜体
UnderlineStyle.NO_UNDERLINE, // 下划线
Colour.BLUE2, // 字体颜色
ScriptStyle.NORMAL_SCRIPT);
WritableCellFormat wcf = new WritableCellFormat(titleWf);
wcf.setBackground(Colour.GRAY_25);// 设置单元格的背景颜色
wcf.setAlignment(Alignment.CENTRE); // 设置对齐方式
wcf.setBorder(Border.ALL, BorderLineStyle.THICK); // 添加边框
cell.setCellFormat(wcf);
在jxl中,有几种常用的数据类型,根据单元格内数据类型的不同,每个WritableCell可以根据其类型被转换为它的一个子类型,以便对不同类型的数据进行专门的处理,通常可以做这样的转换:
- WritableCell fromCell = sheet0.getWritableCell(j, i);
-
- if (fromCell instanceof jxl.write.Number) {
- jxl.write.Number num = (jxl.write.Number) fromCell;
- } else if (fromCell instanceof jxl.write.Boolean) {
- jxl.write.Boolean bool = (jxl.write.Boolean) fromCell;
- } else if (fromCell instanceof jxl.write.DateTime) {
- jxl.write.DateTime dt = (jxl.write.DateTime) fromCell;
- } else if(fromCell instanceof Label){
- Label _label = (Label) fromCell;
- }
WritableCell fromCell = sheet0.getWritableCell(j, i);
if (fromCell instanceof jxl.write.Number) {
jxl.write.Number num = (jxl.write.Number) fromCell;
} else if (fromCell instanceof jxl.write.Boolean) {
jxl.write.Boolean bool = (jxl.write.Boolean) fromCell;
} else if (fromCell instanceof jxl.write.DateTime) {
jxl.write.DateTime dt = (jxl.write.DateTime) fromCell;
} else if(fromCell instanceof Label){
Label _label = (Label) fromCell;
}
当然,操作完成之后一定不要忘了刷新(或者叫写入?)和关掉工作簿:
- workbook.write();
- workbook.close();
workbook.write();
workbook.close();
下面是参考jxl api里那个例子写的,把我自己觉得常用的excel操作基本都包含了:
- package test;
-
- import java.io.File;
- import java.io.IOException;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.Date;
-
- import jxl.CellType;
- import jxl.Workbook;
- import jxl.format.Alignment;
- import jxl.format.Border;
- import jxl.format.BorderLineStyle;
- import jxl.format.Colour;
- import jxl.format.ScriptStyle;
- import jxl.format.UnderlineStyle;
- import jxl.format.VerticalAlignment;
- import jxl.read.biff.BiffException;
- import jxl.write.Blank;
- import jxl.write.DateFormat;
- import jxl.write.DateFormats;
- import jxl.write.DateTime;
- import jxl.write.Formula;
- import jxl.write.Label;
- import jxl.write.Number;
- import jxl.write.NumberFormat;
- import jxl.write.WritableCell;
- import jxl.write.WritableCellFeatures;
- import jxl.write.WritableCellFormat;
- import jxl.write.WritableFont;
- import jxl.write.WritableHyperlink;
- import jxl.write.WritableImage;
- import jxl.write.WritableSheet;
- import jxl.write.WritableWorkbook;
- import jxl.write.WriteException;
-
-
-
-
-
-
- public class ExcelTest {
-
-
-
-
-
-
-
- public static void main(String[] args) throws IOException, BiffException, WriteException {
- Workbook wb = Workbook.getWorkbook(new File("src\\test\\test.xls"));
- WritableWorkbook workbook = Workbook.createWorkbook(new File("d:\\test_modified.xls"),wb);
-
-
-
-
-
- WritableSheet sheet = workbook.getSheet(0);
- sheet.setName("修改后");
- workbook.removeSheet(2);
- workbook.removeSheet(3);
-
- sheet.mergeCells(0, 0, 4, 0);
- sheet.setRowView(0, 600);
- sheet.setColumnView(0, 30);
- sheet.setColumnView(1, 20);
-
- WritableCell cell = null;
- WritableCellFormat wcf = null;
- Label label = null;
- WritableCellFeatures wcfeatures = null;
-
-
- cell = sheet.getWritableCell(0,0);
- WritableFont titleWf = new WritableFont(WritableFont.createFont("仿宋_GB2312"),
- 20,
- WritableFont.NO_BOLD,
- false,
- UnderlineStyle.NO_UNDERLINE,
- Colour.BLUE2,
- ScriptStyle.NORMAL_SCRIPT);
- wcf = new WritableCellFormat(titleWf);
- wcf.setBackground(Colour.GRAY_25);
- wcf.setAlignment(Alignment.CENTRE);
- wcf.setBorder(Border.ALL, BorderLineStyle.THICK);
- cell.setCellFormat(wcf);
-
-
- cell = sheet.getWritableCell(1,2);
- WritableFont fs = new WritableFont(WritableFont.createFont("仿宋_GB2312"),
- 11);
- wcf = new WritableCellFormat(fs);
- cell.setCellFormat(wcf);
-
-
- cell = sheet.getWritableCell(1,3);
- WritableFont size20 = new WritableFont(WritableFont.createFont("宋体"),
- 20);
- wcf = new WritableCellFormat(size20);
- cell.setCellFormat(wcf);
-
-
- cell = sheet.getWritableCell(1,4);
- WritableFont bold = new WritableFont(WritableFont.createFont("宋体"),
- 11,
- WritableFont.BOLD);
- wcf = new WritableCellFormat(bold);
- cell.setCellFormat(wcf);
-
-
- cell = sheet.getWritableCell(1,5);
- WritableFont italic = new WritableFont(WritableFont.createFont("宋体"),
- 11,
- WritableFont.NO_BOLD,
- true);
- wcf = new WritableCellFormat(italic);
- cell.setCellFormat(wcf);
-
-
- cell = sheet.getWritableCell(1,6);
- WritableFont underline = new WritableFont(WritableFont.createFont("宋体"),
- 11,
- WritableFont.NO_BOLD,
- false,
- UnderlineStyle.SINGLE);
- wcf = new WritableCellFormat(underline);
- cell.setCellFormat(wcf);
-
-
- cell = sheet.getWritableCell(1,7);
- if (cell.getType() == CellType.LABEL)
- {
- Label lc = (Label) cell;
- lc.setString(lc.getString() + " - 已修改");
- }
-
-
- cell = sheet.getWritableCell(1,8);
- WritableFont align = new WritableFont(WritableFont.createFont("宋体"),
- 11);
- wcf = new WritableCellFormat(align);
- wcf.setAlignment(Alignment.RIGHT);
- wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
- cell.setCellFormat(wcf);
-
-
- cell = sheet.getWritableCell(4,2);
- WritableFont justify = new WritableFont(WritableFont.createFont("宋体"),
- 11);
- wcf = new WritableCellFormat(justify);
- wcf.setAlignment(Alignment.JUSTIFY);
- cell.setCellFormat(wcf);
-
-
-
- cell = sheet.getWritableCell(1,11);
- NumberFormat sevendps = new NumberFormat("#.0000000");
- wcf = new WritableCellFormat(sevendps);
- cell.setCellFormat(wcf);
-
-
- cell = sheet.getWritableCell(1,12);
- NumberFormat exp4 = new NumberFormat("0.####E0");
- wcf = new WritableCellFormat(exp4);
- cell.setCellFormat(wcf);
-
-
- cell = sheet.getWritableCell(1,13);
- cell.setCellFormat(WritableWorkbook.NORMAL_STYLE);
-
-
- cell = sheet.getWritableCell(1,14);
- if (cell.getType() == CellType.NUMBER)
- {
- Number n = (Number) cell;
- n.setValue(42);
- }
-
-
- cell = sheet.getWritableCell(1,15);
- if (cell.getType() == CellType.NUMBER)
- {
- Number n = (Number) cell;
- n.setValue(n.getValue() + 0.1);
- }
-
-
- cell = sheet.getWritableCell(1,18);
- wcf = new WritableCellFormat(DateFormats.FORMAT9);
- cell.setCellFormat(wcf);
-
-
- cell = sheet.getWritableCell(1,19);
- DateFormat df = new DateFormat("dd MMM yyyy HH:mm:ss");
- wcf = new WritableCellFormat(df);
- cell.setCellFormat(wcf);
-
-
- cell = sheet.getWritableCell(1,20);
- if (cell.getType() == CellType.DATE)
- {
- DateTime dt = (DateTime) cell;
- Calendar cal = Calendar.getInstance();
- cal.set(2011, 5, 1, 11, 18, 50);
- Date d = cal.getTime();
- dt.setDate(d);
- }
-
-
-
- WritableHyperlink link = new WritableHyperlink(1, 23, new URL("http://www.baidu.com"));
- sheet.addHyperlink(link);
-
-
- WritableHyperlink hyperlinks[] = sheet.getWritableHyperlinks();
- for (int i = 0; i < hyperlinks.length; i++) {
- WritableHyperlink wh = hyperlinks[i];
- if (wh.getColumn() == 1 && wh.getRow() == 24) {
-
- sheet.removeHyperlink(wh,true);
- }else if(wh.getColumn() == 1 && wh.getRow() == 25){
- try {
-
- wh.setURL(new URL("http://wuhongyu.javaeye.com"));
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
- }
- }
-
-
-
- Formula f1 = new Formula(1, 28, "SUM(C29:D29)");
- sheet.addCell(f1);
- Formula f2 = new Formula(1, 29, "AVERAGE(C30:G30)");
- sheet.addCell(f2);
-
-
- File file = new File("d:\\shu05.png");
- WritableImage image = new WritableImage(1, 31, 3, 10, file);
- sheet.addImage(image);
-
-
- label = new Label(0, 43, "Added drop down validation");
- wcfeatures = new WritableCellFeatures();
- wcfeatures.setComment("右边列是个下拉列表");
- label.setCellFeatures(wcfeatures);
- sheet.addCell(label);
-
-
- Blank b = new Blank(1, 43);
- wcfeatures = new WritableCellFeatures();
- ArrayList al = new ArrayList();
- al.add("why");
- al.add("landor");
- al.add("tjm");
- wcfeatures.setDataValidationList(al);
- wcfeatures.setComment("这是一个注释");
- b.setCellFeatures(wcfeatures);
- sheet.addCell(b);
-
-
-
- cell = sheet.getWritableCell(0,45);
- wcfeatures = new WritableCellFeatures();
- wcfeatures.setComment("这个注释不会被显示,删了这行还不行,MD");
- cell.setCellFeatures(wcfeatures);
-
- label = (Label) cell;
-
- label.addCellFeatures();
- label.getWritableCellFeatures().setComment("终于加上注释了,哈哈哈哈");
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- workbook.write();
- workbook.close();
- wb.close();
- }
-
- }
package test;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import jxl.CellType;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.ScriptStyle;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.read.biff.BiffException;
import jxl.write.Blank;
import jxl.write.DateFormat;
import jxl.write.DateFormats;
import jxl.write.DateTime;
import jxl.write.Formula;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.NumberFormat;
import jxl.write.WritableCell;
import jxl.write.WritableCellFeatures;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableHyperlink;
import jxl.write.WritableImage;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
/**
*
* @author why
*
*/
public class ExcelTest {
/**
* @param args
* @throws IOException
* @throws BiffException
* @throws WriteException
*/
public static void main(String[] args) throws IOException, BiffException, WriteException {
Workbook wb = Workbook.getWorkbook(new File("src\\test\\test.xls")); // 获得原始文档
WritableWorkbook workbook = Workbook.createWorkbook(new File("d:\\test_modified.xls"),wb); // 创建一个可读写的副本
/**
* 定义与设置Sheet
*/
WritableSheet sheet = workbook.get