jexcelapi java对excel的操作

已经转移到 好·色之徒--我的博客、我的生活

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 区域名称(这个和具体业务有关) 这里只是举了个实际项目中的例子,仅作参考 已经转移到 好·色之徒--我的博客、我的生活

你可能感兴趣的:(jexcelapi java对excel的操作)