1.使用poi生成文件
package com.mi.entity; import java.util.Date; public class Student { private int id; private String name; private int age; private Date birth; public Student(int id, String name, int age, Date birth) { super(); this.id = id; this.name = name; this.age = age; this.birth = birth; } }
生成excel文件代码
package com.mi.util; import java.io.File; import java.io.FileOutputStream; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import com.mi.entity.Student; public class CreateSimpleExcelToDisk { /** * 手工创建一个包含student的list * @return * @throws Exception */ private static List<Student> getStudent() throws Exception { List<Student> list = new ArrayList<>(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd"); Student user1 = new Student(1, "张三", 16, sdf.parse("1997-03-12")); Student user2 = new Student(2, "李四", 17, sdf.parse("1996-08-12")); Student user3 = new Student(3, "王五", 26, sdf.parse("1985-11-12")); list.add(user1); list.add(user2); list.add(user3); return list; } public static void main(String[] args) throws Exception { //第一步,创建一个webbook文件,对应一个excel文件 HSSFWorkbook wb = new HSSFWorkbook(); //第二部,在excel中添加一个sheet工作簿,参数为该工作簿名字,不写为默认; HSSFSheet sheet = wb.createSheet("学生表1"); //第三部,做sheet中添加表头第0行,注意老版本poi对excel的行数列数有限制short HSSFRow row = sheet.createRow((int)0); //第四部,创建单元格表头 设置表头居中 HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//创建一个居中格式 //创建具体盛放数据的单元格,可以考虑把cell抽成共通对象去使用 HSSFCell cell = row.createCell((int) 0); cell.setCellValue("学号"); cell = row.createCell((int) 1); cell.setCellValue("姓名"); cell = row.createCell((int) 2); cell.setCellValue("年龄"); cell = row.createCell((int) 3); cell.setCellValue("生日"); //第五部,写入实体数据 实际应用中这些数据应该是从数据库中得到 List<Student> list = CreateSimpleExcelToDisk.getStudent(); for(int i=0;i<list.size();i++){ //每次新建一行然后在新行中插入list中的数据对象,有点繁琐,也许有更好的封装方法,留待后看 row = sheet.createRow((int)i+1); row.createCell((int)0).setCellValue(list.get(i).getId()); row.createCell((int)1).setCellValue(list.get(i).getName()); row.createCell((int)2).setCellValue(list.get(i).getAge()); row.createCell((int)3).setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(list.get(i).getBirth())); } //第六部,将文件保存到指定位置 FileOutputStream fout = new FileOutputStream("D:/student.xls"); wb.write(fout); fout.close(); } }
效果:
注:.首先下载poi-3.6-20091214.jar,下载地址如下:
http://download.csdn.net/detail/evangel_z/3895051
2.使用JXL生成文件
package com.mi.util; import java.io.File; import jxl.Workbook; import jxl.format.Border; import jxl.format.BorderLineStyle; import jxl.format.Colour; import jxl.write.Label; import jxl.write.WritableCellFormat; import jxl.write.WritableFont; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; public class JxlExcelCreateTool { public static void main(String[] args) throws Exception { writeExcel(); } public static void writeExcel() throws Exception { // 第一步创建excel文件,并决定excel的路径和文件名 WritableWorkbook wwb = Workbook.createWorkbook(new File("D:/hello.xls")); // 第二部,创建工作簿,指定该工作簿的名称和位置' WritableSheet sheet = wwb.createSheet("test sheet1", 0); // 第三部,设置表格指定列的列宽 sheet.setColumnView(0, 14); sheet.setColumnView(1, 12); sheet.setColumnView(2, 25); sheet.setColumnView(3, 20); sheet.setColumnView(4, 12); sheet.setColumnView(5, 9); // 第四部,往工作簿中插入数据,设定字体:微软雅黑,24,加粗 // 创建字体对象 WritableFont titleFont = new WritableFont(WritableFont.createFont("微软雅黑"), 24, WritableFont.NO_BOLD); WritableFont contentFont = new WritableFont(WritableFont.createFont("楷体 _GB2312"), 12, WritableFont.NO_BOLD); WritableCellFormat titleFormat = new WritableCellFormat(titleFont); WritableCellFormat contentFormat = new WritableCellFormat(contentFont); WritableCellFormat contentFormat2 = new WritableCellFormat(contentFont); contentFormat.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK); // 设置格式居中对齐 titleFormat.setAlignment(jxl.format.Alignment.CENTRE); contentFormat.setAlignment(jxl.format.Alignment.CENTRE); contentFormat2.setAlignment(jxl.format.Alignment.CENTRE); // 将定义好的单元格绑定数据添加到工作簿中 sheet.mergeCells(0, 0, 6, 0); // 合并单元格A-G列共7列 sheet.addCell(new Label(0, 0, "广州XXX大学2009级研究生课程考试成绩册", titleFormat)); sheet.addCell(new Label(0, 1, "课程名称", contentFormat2)); sheet.mergeCells(1, 1, 6, 1); // 合并单元格B-G列共7列 sheet.addCell(new Label(1, 1, "大学数学", contentFormat2)); sheet.addCell(new Label(0, 2, "院所教研室", contentFormat2)); sheet.mergeCells(1, 2, 6, 2); // 合并单元格B-G列共7列 sheet.addCell(new Label(0, 3, "填表人", contentFormat2)); sheet.addCell(new Label(2, 3, "教研室负责人", contentFormat2)); String th[] = { "学号", "姓名", "学院", "平时成绩", "期末成绩", "总成绩", "补考成绩" }; for (int i = 0; i < th.length; i++) { sheet.addCell(new Label(i, 4, th[i], contentFormat2)); } // 这里的数据可以从数据库里获取,个人建议将这些抽成共通的方法,提供导入数据,用时调用即可 int xh = 200901; String xm = "王佳佳"; String xy = "XXX信息技术学院"; String space = " "; int cj = 50; String bk = "补 80"; for (int j = 5; j < 10; j++) { sheet.addCell(new Label(0, j, "" + xh + j + "", contentFormat)); sheet.addCell(new Label(1, j, xm + j, contentFormat)); sheet.addCell(new Label(2, j, xy, contentFormat)); sheet.addCell(new Label(3, j, space, contentFormat)); sheet.addCell(new Label(4, j, space, contentFormat)); sheet.addCell(new Label(5, j, "" + cj + j + "", contentFormat)); sheet.addCell(new Label(6, j, "" + bk + "", contentFormat)); } // 第五部,写入工作表完毕,关闭流 wwb.write(); wwb.close(); } }
效果: