使用JExcel导出excel文件
2013-11-13 16:04
239 查看
package org.aaa.portal.tools; import java.io.File; import java.io.IOException; import java.util.List; import java.util.Map; import jxl.Workbook; import jxl.format.Alignment; import jxl.format.Border; import jxl.format.BorderLineStyle; import jxl.format.VerticalAlignment; import jxl.write.Label; import jxl.write.Number; import jxl.write.WritableCellFormat; import jxl.write.WritableFont; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import jxl.write.WriteException; import jxl.write.biff.RowsExceededException; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class ExcelUtils { public static final String COLUMN_KEY="key"; //列的键,对应JSONObject的key public static final String COLUMN_TEXT="text"; //列头名称 /** * 把JSONArray存储为Excel * @param fileName 完整文件路径名称 * @param sheetName sheet 名称 * @param ja JSONArray * @param columns * List<Map<String, String>> 按照list Item的顺序生成列 * Map格式: Map<String,String> column1 =new HashMap<String,String>(); column1.put(ExcelUtils.COLUMN_KEY, "sUserName"); column1.put(ExcelUtils.COLUMN_TEXT, "用户名"); columns.add(column1); */ public static boolean exportFromJSONArray(String fileName, String sheetName,JSONArray ja, List<Map<String, String>> columns) { boolean isSuccess=true; WritableWorkbook book; try { //确保保存文件目录存在 String filePathStr=fileName.substring(0,fileName.lastIndexOf(File.separatorChar)); File filePath=new File(filePathStr); if(!filePath.exists()){ filePath.mkdirs(); } //确保保存文件存在 File file=new File(fileName); if(!file.exists()){ file.createNewFile(); } // 在指定地址创建EXCEL表 book = Workbook.createWorkbook(file); // 设置第一个工作薄的名字 WritableSheet sheet = book.createSheet(sheetName, 0); // 设置列头 for (int i = 0; i < columns.size(); i++) { // 设置列宽 sheet.setColumnView(i, 20); // 设置列头样式 // 分别设置表头,表元的格式 WritableFont fontb = new WritableFont(WritableFont.ARIAL, 8, WritableFont.BOLD, false); // 粗体 WritableCellFormat formatb = new WritableCellFormat(fontb); formatb.setAlignment(Alignment.CENTRE); // 水平居中 formatb.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直居中 formatb.setBorder(Border.ALL, BorderLineStyle.THIN); // 边框 // 填充列名 Label labelb = new Label(i, 0, columns.get(i).get("text"), formatb); sheet.addCell(labelb); } //设置数据单元格格式 WritableFont fontc = new WritableFont(WritableFont.ARIAL, 8, WritableFont.NO_BOLD, false); WritableCellFormat formatc = new WritableCellFormat(fontc); formatc.setAlignment(Alignment.CENTRE); formatc.setVerticalAlignment(VerticalAlignment.CENTRE); formatc.setBorder(Border.ALL, BorderLineStyle.THIN); // 填充数据 for (int i = 0; i < ja.size(); i++) { JSONObject jo = ja.getJSONObject(i); // 按照预设的列顺序填充 for (int j = 0; j < columns.size(); j++) { String key = columns.get(j).get("key"); Object text = jo.get(key); // 如果是数字,则按照数字的格式填充 if (text != null && text.toString().matches("\\d+")) { Number numberc = new Number(j, i+1, Long.parseLong(text.toString()), formatc); sheet.addCell(numberc); } else { //其余全按文字处理 Label labelb = new Label(j, i+1, text.toString(), formatc); sheet.addCell(labelb); } } } book.write(); book.close(); } catch (IOException e) { e.printStackTrace(); isSuccess=false; } catch (RowsExceededException e) { e.printStackTrace(); isSuccess=false; } catch (WriteException e) { e.printStackTrace(); isSuccess=false; } return isSuccess; } }
相关文章推荐
- 使用owc的spreadsheet导出Excel文件时,格式丢失的解决方法
- 使用JXL组件操作Excel和导出文件
- Asp.Net MVC5 使用NPIO导出Excel数据文件方法总结
- 直接将数据导出到Excel文件,直接操作文件没有使用Variant
- 使用phpExcel类导出excel文件相关问题总结
- MVC 使用FileResult导出Excel数据文件
- 使用jxl读取模板文件,写入数据并导出excel
- CI框架中使用PHPExcel导出Excel文件
- 使用响应流来输出文件(适合导出文件使用,本文主要测试的是导出excel)
- Yii框架中使用PHPExcel导出Excel文件
- 使用JXL组件操作Excel和导出文件
- 使用oledb导出2007格式的Excel文件
- 【工具篇】Excel文件导出从未如此简单——EasyPOI的使用
- net控件中数据导到Excel的格式 首先,我们了解一下excel从web页面上导出的原理。当我们把这些数据发送到客户端时,我们想让客户端程序(浏览器)以excel的格式读取它,所以把mime类型设为:application/vnd.ms-excel,当excel读取文件时会以每个cell的格式呈现数据,如果cell没有规定的格式,则excel会以默认的格式去呈现该cell的数据。这样就给我们提供了自定义数据格式的空间,当然我们必须使用excel支持的格式。下面就列出常用的一些格式: 1) 文本
- 使用PHP导入Excel和导出数据为Excel文件
- 在使用 ADO.NET 导出 Excel 文件时,设置 IMEX=1 仍不能导出所有数据的解决办法
- python3使用openpyxl导出巨大量数据到excel文件
- 使用jexcel读写excel文件的例子
- java中使用JXL对Excel文件进行数据的写入、导出操作
- 使用NPOI导出Excel文件