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(); } }
效果: