1.首先,創建一個可讀寫的工作簿(WritableWorkbook):
- WritableWorkbook workbook = Workbook.createWorkbook(new File("d:\\test.xls"));
2.如果是想要修改一個已存在的excel工作簿,則需要先獲得它的原始工作簿,再創建一個可讀寫的副本:
- Workbook wb = Workbook.getWorkbook(new File("src\\test\\test.xls")); // 獲得原始文檔
- WritableWorkbook workbook = Workbook.createWorkbook(new File("d:\\test_modified.xls"),wb); // 創建一個可讀寫的副本
3.取得要操作的sheet,並對其進行相應的操作,如改名、合併單元格、設置列寬、行高等:
- sheet.mergeCells(0, 0, 4, 0); // 合併單元格
- sheet.setRowView(0, 600); // 設置行的高度
- sheet.setColumnView(0, 30); // 設置列的寬度
- sheet.setColumnView(1, 20); // 設置列的寬度
4.通過WritableFont、WritableCellFormat等對象可以設置單元格的字體、樣式等外觀:
- WritableSheet sheet = workbook.getSheet(0); 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);
- sheet.setName("修改後"); // 給sheet頁改名
- workbook.removeSheet(2); // 移除多餘的標籤頁
- workbook.removeSheet(3);
demo1:
- public String createExcel(String path,String filename,List list,TblKepuoaRecordDispatch dispatch) throws Exception{
- File file = new File(path+File.separator+filename+".xls" );
- WritableWorkbook wbook = null;
- try {
- wbook = Workbook.createWorkbook(file);
- // 建立excel文件
- String tmptitle = filename; // 標題
- WritableSheet wsheet = wbook.createSheet(filename, 0); // sheet名稱
- // 設置excel標題
- WritableFont wfont = new WritableFont(WritableFont.createFont("宋體"), 16, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
- WritableCellFormat wcfFC = new WritableCellFormat(wfont);
- wcfFC.setAlignment(Alignment.CENTRE); // 設置對齊方式
- wsheet.mergeCells(0, 0, 9, 0); // 合併單元格
- wsheet.addCell(new Label(0, 0, tmptitle, wcfFC));
- wfont = new jxl.write.WritableFont(WritableFont.createFont("宋體"), 13,WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
- wcfFC = new WritableCellFormat(wfont);
- wcfFC.setBorder(Border.NONE,BorderLineStyle.NONE);
- wcfFC.setAlignment(Alignment.CENTRE); // 設置對齊方式
- wsheet.setColumnView(0, 6); // 設置列的寬度
- wsheet.setColumnView(1, 20); // 設置列的寬度
- wsheet.setColumnView(2, 25); // 設置列的寬度
- wsheet.setColumnView(3, 60); // 設置列的寬度
- wsheet.setColumnView(4, 10); // 設置列的寬度
- wsheet.setColumnView(5, 6); // 設置列的寬度
- wsheet.setColumnView(6, 6); // 設置列的寬度
- wsheet.setColumnView(7, 80); // 設置列的寬度
- wsheet.setColumnView(8, 20); // 設置列的寬度
- wsheet.setColumnView(9, 20); // 設置列的寬度
- // 開始生成主體內容
- wsheet.addCell(new Label(0, 1, "序號",wcfFC));
- wsheet.addCell(new Label(1, 1, "日期",wcfFC));
- wsheet.addCell(new Label(2, 1, "發文編號",wcfFC));
- wsheet.addCell(new Label(3, 1, "收文單位",wcfFC));
- wsheet.addCell(new Label(4, 1, "文件類型",wcfFC));
- wsheet.addCell(new Label(5, 1, "件數",wcfFC));
- wsheet.addCell(new Label(6, 1, "附件",wcfFC));
- wsheet.addCell(new Label(7, 1, "文件標題",wcfFC));
- wsheet.addCell(new Label(8, 1, "存檔編號",wcfFC));
- wsheet.addCell(new Label(9, 1, "簽字",wcfFC));
- wfont = new jxl.write.WritableFont(WritableFont.createFont("宋體"), 13,WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
- wcfFC = new WritableCellFormat(wfont);
- wcfFC.setBorder(Border.NONE,BorderLineStyle.NONE);
- int j = 2;
- for (int i = 0; i < list.size(); i++) {
- dispatch=(TblKepuoaRecordDispatch) list.get(i);
- String uptime="";
- if(dispatch.getDispatchDate()!=null&&!"".equals(dispatch.getDispatchDate())){
- uptime=sdf2.format(dispatch.getDispatchDate());//時間格式化
- }
- wsheet.addCell(new Label(0, j, i+1+"",wcfFC));
- wsheet.addCell(new Label(1, j,uptime,wcfFC));
- wsheet.addCell(new Label(2, j,dispatch.getDispatchNo(),wcfFC));
- wsheet.addCell(new Label(3, j,dispatch.getDispatchOrgan(),wcfFC));
- /*if(dispatch!=null && dispatch.getDispatchOpreator()!=null && !"".equals(dispatch.getDispatchOpreator())){
- if(dispatch.getCode()!=null && !"".equals(dispatch.getCode())){
- wsheet.addCell(new Label(4, j,dispatch.getDispatchOpreator()+"-"+dispatch.getCode(),wcfFC));
- }else{
- wsheet.addCell(new Label(4, j,dispatch.getDispatchOpreator(),wcfFC));
- }
- }*/
- wsheet.addCell(new Label(4, j, dispatch.getDispatchType(),wcfFC));
- wsheet.addCell(new Label(5, j,String.valueOf(dispatch.getDispatchCount()),wcfFC));
- wsheet.addCell(new Label(6, j,String.valueOf(dispatch.getDispatchAccessoriescCount()),wcfFC));
- wsheet.addCell(new Label(7, j,dispatch.getDispatchName(),wcfFC));
- wsheet.addCell(new Label(8, j, dispatch.getArchiveNo(),wcfFC));
- j++;
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- Loggers.info("DispatchAction 中 createExcel 方法:"+e.getMessage());
- e.printStackTrace();
- }finally{
- if(wbook != null){
- wbook.write(); // 寫入文件
- wbook.close();
- }
- }
- // 主體內容生成結束
- return filename+".xls";
- }
下面是參考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;
- /**
- *
- * @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.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); // 設置列的寬度
- 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.DEFAULT_POINT_SIZE, // 字號
- 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);
- // 將B3的字體改爲仿宋_GB2312
- cell = sheet.getWritableCell(1,2);
- WritableFont fs = new WritableFont(WritableFont.createFont("仿宋_GB2312"),
- 11);
- wcf = new WritableCellFormat(fs);
- cell.setCellFormat(wcf);
- // 將B4的字號改爲20
- cell = sheet.getWritableCell(1,3);
- WritableFont size20 = new WritableFont(WritableFont.createFont("宋體"),
- 20);
- wcf = new WritableCellFormat(size20);
- cell.setCellFormat(wcf);
- // 將B5的字體改爲加粗
- cell = sheet.getWritableCell(1,4);
- WritableFont bold = new WritableFont(WritableFont.createFont("宋體"),
- 11,
- WritableFont.BOLD);
- wcf = new WritableCellFormat(bold);
- cell.setCellFormat(wcf);
- // 將B6的字體改爲傾斜
- cell = sheet.getWritableCell(1,5);
- WritableFont italic = new WritableFont(WritableFont.createFont("宋體"),
- 11,
- WritableFont.NO_BOLD,
- true);
- wcf = new WritableCellFormat(italic);
- cell.setCellFormat(wcf);
- // 將B7字體加下劃線
- 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);
- // 將B8的文字改爲“待修改文字-已修改”
- cell = sheet.getWritableCell(1,7);
- if (cell.getType() == CellType.LABEL)
- {
- Label lc = (Label) cell;
- lc.setString(lc.getString() + " - 已修改");
- }
- // 將B9文字對齊方式改爲垂直居中、右對齊
- 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);
- // 將E3文字改爲自動換行
- cell = sheet.getWritableCell(4,2);
- WritableFont justify = new WritableFont(WritableFont.createFont("宋體"),
- 11);
- wcf = new WritableCellFormat(justify);
- wcf.setAlignment(Alignment.JUSTIFY);
- cell.setCellFormat(wcf);
- // 將B12的數字有效位數從5位改爲7位
- cell = sheet.getWritableCell(1,11);
- NumberFormat sevendps = new NumberFormat("#.0000000");
- wcf = new WritableCellFormat(sevendps);
- cell.setCellFormat(wcf);
- // 將B13改爲4位科學計數法表示
- cell = sheet.getWritableCell(1,12);
- NumberFormat exp4 = new NumberFormat("0.####E0");
- wcf = new WritableCellFormat(exp4);
- cell.setCellFormat(wcf);
- // 將B14改爲默認數字表示
- cell = sheet.getWritableCell(1,13);
- cell.setCellFormat(WritableWorkbook.NORMAL_STYLE);
- // 將B15數字類型的值17改爲22
- cell = sheet.getWritableCell(1,14);
- if (cell.getType() == CellType.NUMBER)
- {
- Number n = (Number) cell;
- n.setValue(42);
- }
- // 將B16的值2.71進行加法運算2.71 + 0.1
- cell = sheet.getWritableCell(1,15);
- if (cell.getType() == CellType.NUMBER)
- {
- Number n = (Number) cell;
- n.setValue(n.getValue() + 0.1);
- }
- // 將B19日期格式改爲默認
- cell = sheet.getWritableCell(1,18);
- wcf = new WritableCellFormat(DateFormats.FORMAT9);
- cell.setCellFormat(wcf);
- // 將B20日期格式改爲dd MMM yyyy HH:mm:ss
- cell = sheet.getWritableCell(1,19);
- DateFormat df = new DateFormat("dd MMM yyyy HH:mm:ss");
- wcf = new WritableCellFormat(df);
- cell.setCellFormat(wcf);
- // 將B21的日期設置爲 2011-6-1 11:18:50
- 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);
- }
- // 將B24文字添加鏈接http://www.baidu.com
- WritableHyperlink link = new WritableHyperlink(1, 23, new URL("http://www.baidu.com"));
- sheet.addHyperlink(link);
- // 更改URL鏈接
- WritableHyperlink hyperlinks[] = sheet.getWritableHyperlinks();
- for (int i = 0; i < hyperlinks.length; i++) {
- WritableHyperlink wh = hyperlinks[i];
- if (wh.getColumn() == 1 && wh.getRow() == 24) {
- // 將B25文字鏈接取消
- sheet.removeHyperlink(wh,true);//true:保留文字;false:刪除文字
- }else if(wh.getColumn() == 1 && wh.getRow() == 25){
- try {
- // 將B26鏈接更改爲http://wuhongyu.javaeye.com
- wh.setURL(new URL("http://wuhongyu.javaeye.com"));
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
- }
- }
- // 利用公式取得B29、B30的值
- Formula f1 = new Formula(1, 28, "SUM(C29:D29)");
- sheet.addCell(f1);
- Formula f2 = new Formula(1, 29, "AVERAGE(C30:G30)");
- sheet.addCell(f2);
- // 在B32處添加圖片,圖片大小佔10行3列,只支持png格式
- File file = new File("d:\\shu05.png");
- WritableImage image = new WritableImage(1, 31, 3, 10, file);
- sheet.addImage(image);
- // 在A44出添加內容"Added drop down validation",併爲其添加註釋
- label = new Label(0, 43, "Added drop down validation");
- wcfeatures = new WritableCellFeatures();
- wcfeatures.setComment("右邊列是個下拉列表");
- label.setCellFeatures(wcfeatures);
- sheet.addCell(label);
- // 在B44處添加一個下拉列表並添加註釋
- 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);
- // 爲A46添加註釋。
- // 此處比較麻煩,試了多次發現必須將cell強制類型轉換、添加CellFeatures再修改註釋纔可用,不知有沒有更好的辦法。
- cell = sheet.getWritableCell(0,45);
- wcfeatures = new WritableCellFeatures();
- wcfeatures.setComment("這個註釋不會被顯示,刪了這行還不行,MD");
- cell.setCellFeatures(wcfeatures);
- label = (Label) cell;
- // label.setCellFeatures(wcfeatures);// 直接這樣寫會報一個警告(“註釋已存在”),但那個註釋仍會被顯示。
- label.addCellFeatures();
- label.getWritableCellFeatures().setComment("終於加上註釋了,哈哈哈哈");
- // if (cell instanceof Number) {
- // Number num = (Number) cell;
- // num.setCellFeatures(wcfeatures);
- // } else if (cell instanceof jxl.write.Boolean) {
- // jxl.write.Boolean bool = (jxl.write.Boolean) cell;
- // bool.setCellFeatures(wcfeatures);
- // } else if (cell instanceof jxl.write.DateTime) {
- // jxl.write.DateTime dt = (jxl.write.DateTime) cell;
- // dt.setCellFeatures(wcfeatures);
- // } else {
- // Label _label = (Label) cell;
- // _label.setCellFeatures(wcfeatures);
- // }
- workbook.write();
- workbook.close();
- wb.close();
- }
- }
http://www.andykhan.com/jexcelapi/download.html Jxl--jar包下載地址