package lee; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.Date; import java.util.List; import jxl.Workbook; import jxl.format.Alignment; import jxl.format.Border; import jxl.format.BorderLineStyle; import jxl.format.Colour; import jxl.format.Pattern; import jxl.format.UnderlineStyle; import jxl.format.VerticalAlignment; import jxl.write.Label; import jxl.write.WritableCellFormat; import jxl.write.WritableFont; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import jxl.write.WriteException; /** * 导出Excel文件 * * @author MrGao 2010-7-13下午02:19:51 */ public class ExcelExport { public static void main(String[] args) { List list = new ArrayList(); list.add(new String[] { "a", "b", "c" }); list.add(new String[] { "a", "b", "c" }); System.out.println(ExcelExport.class.getResource("")); createExcel(list, new String[] { "ID", "名称", "时间" }, "c:\\", String.valueOf(new Date().getTime())); } /** * 生成Excel * * @param models * 数据List<String[]> * @param className * 导成Excel的实体列名组String[] * @param tempPath * 生成Excel存放的临时路径String * @param excelName * 生成的Excel名String */ public static void createExcel(List models, String[] classNames, String tempPath, String excelName) { // Class clasVo = null; try { // clasVo = Class.forName(className); OutputStream os = new FileOutputStream(tempPath + "\\" + excelName + ".xls"); WritableWorkbook workbook = Workbook.createWorkbook(os); WritableSheet sheet = workbook.createSheet(excelName, 0); // 用于标题 WritableFont titleFont = new WritableFont(WritableFont.ARIAL, 17, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.WHITE); WritableCellFormat wcf_title = new WritableCellFormat(titleFont); wcf_title.setBackground(Colour.TEAL, Pattern.SOLID); wcf_title.setBorder(Border.ALL, BorderLineStyle.DOUBLE, Colour.OCEAN_BLUE); wcf_title.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直对齐 wcf_title.setAlignment(Alignment.CENTRE); // 用于正文 WritableFont NormalFont = new WritableFont(WritableFont.TAHOMA, 11); WritableCellFormat wcf_center = new WritableCellFormat(NormalFont); wcf_center.setBorder(Border.ALL, BorderLineStyle.DOUBLE, Colour.GRAY_25); wcf_center.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直对齐 wcf_center.setAlignment(Alignment.CENTRE); wcf_center.setWrap(true); // 是否换行 sheet.addCell(new Label(0, 0, excelName, wcf_title)); sheet.mergeCells(0, 0, classNames.length, 0); //设置列名 for (int i = 0; i < classNames.length; i++) { sheet.setColumnView(i, classNames[i].length() * 5); sheet.addCell(new Label(i, 1, classNames[i], wcf_center)); } int rowId = 2;// 写入第几行 第一行为列头 数据从第二行开始写 //导入数据集 for (Object ssTopModel : models) { int columnId = 0;// 写入第几列 第一列为自动计算的行号 数据从第二列开始写 // 获取该类 并获取自身方法 String[] strs = (String[]) ssTopModel; for (int i = 0; i < strs.length; i++) { try { sheet.addCell(new Label(columnId, rowId, strs[i], wcf_center)); } catch (Exception e) { e.printStackTrace(); } columnId++; } rowId++; } workbook.write(); workbook.close(); os.flush(); os.close(); } catch (WriteException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
?