Posted on 2006-09-30 16:17
城市劣人 閱讀(757)
評論(0) 編輯 收藏
已經轉移到
好·色之徒--我的博客、我的生活Java Excel API(JXL)是一個成熟,開源的Excel電子表格讀取,修改,寫入的項目。Java開發者利用它簡單,便利的API就能夠讀取Excel電子表格,進行修改并能夠把修改后的變化寫到任何output stream中(如:Disk, HTTP, database, 或任何socket),這個項目基于GPL發布,而且對中文有很好的支持。 ● 支持字體、數字、日期操作 ● 能夠修飾單元格屬性 ● 支持圖像和圖表 [color=blue]備注:對中文寫支持,需要jdk1.5,低版本的jdk或有問題[/color] 這里不作多的介紹,可以上網檢索一下,會有很多相關介紹,下面舉個具體的例子: public class ExcleServiceImpl extends BaseService implements IExcleService { /** 行 * */ private static final int ROW = 6; /** 列 * */ private static final int COL = 0; public int makeExcle(List okExcle, String sourceFile, String aimFile, String areaName) { if (okExcle.isEmpty()) { return -1; } int result = 1; try { Workbook workbook = Workbook.getWorkbook(new File(sourceFile)); // 拷貝一份excle模版文件 WritableWorkbook copy = Workbook.createWorkbook(new File(aimFile), workbook); WritableSheet sheet = null; // 添加的字體樣式 WritableFont wf = new WritableFont(WritableFont.TIMES, 16, WritableFont.BOLD, true); WritableCellFormat wcfF = new WritableCellFormat(wf); WritableFont wfTitle = new WritableFont(WritableFont.ARIAL, 12); WritableCellFormat wcfFTitle = new WritableCellFormat(wfTitle); wcfFTitle.setBackground(Colour.PALE_BLUE); wcfFTitle.setBorder(Border.ALL, BorderLineStyle.THIN); // 做成處理完畢訂單的excle sheet = copy.getSheet(1); Label labelOK = new Label(2, 1, areaName, wcfF); sheet.addCell(labelOK); sheet.addCell(new Label(0, 4, "單號", wcfFTitle)); sheet.addCell(new Label(1, 4, "訂單日期", wcfFTitle)); sheet.addCell(new Label(2, 4, "客戶名稱(工程)", wcfFTitle)); sheet.addCell(new Label(3, 4, "發貨倉庫", wcfFTitle)); sheet.addCell(new Label(4, 4, "打印裝箱單時間", wcfFTitle)); makeOKExcle(okExcle, sheet); copy.write(); copy.close(); workbook.close(); } catch (Exception e) { result = 0; logger.error("Method:makeExcle(...):做成Excle失敗!", e); } return result; } private void makeOKExcle(List list, WritableSheet sheet) { BasicDynaBean bean; List valueList = new ArrayList(); Label label = null; String orderno = ""; String orderdate = ""; String customername = ""; String warehouseid = ""; String printingdate = ""; String handmade = ""; String handmadeFlg = ""; if (list.isEmpty()) { return; } try { for (int i = 0; i < list.size(); i++) { bean = (BasicDynaBean) list.get(i); orderno = convert(bean.get("orderno")); orderdate = convert(bean.get("orderdate")); customername = convert(bean.get("customername")); warehouseid = convert(bean.get("warehouseid")); printingdate = convert(bean.get("printingdate")); handmade = convert(bean.get("handmade")); handmadeFlg = convert(bean.get("handmadeflg")); valueList.add(orderno); valueList.add(orderdate); valueList.add(customername); valueList.add(warehouseid); if (handmadeFlg != null && "OK".equals(handmadeFlg)) { valueList.add(handmade); } else { valueList.add(printingdate); } for (int j = 0; j < valueList.size(); j++) { label = new Label(COL + j, ROW + i, (String) valueList .get(j)); sheet.addCell(label); } valueList.clear(); } } catch (Exception e) { logger.error("Method:makeOKExcle(...):做成處理完畢的訂單Excle失敗!", e); } } private String convert(Object object) { String str = ""; if (object != null && !"".equals(object)) { str = object.toString(); } return str; } } List okExcle 是需要寫入excel文件的數據源 String sourceFile 源文件 String aimFile 目標文件 String areaName 區域名稱(這個和具體業務有關) 這里只是舉了個實際項目中的例子,僅作參考 已經轉移到
好·色之徒--我的博客、我的生活