首页 > 代码库 > Java将list数据导出到Excel——(八)
Java将list数据导出到Excel——(八)
Java实体类
1 package bean; 2 3 public class Question { 4 5 private String timu; //题干 6 private String leixing; //类型 7 private String axuanxiang; //A选项 8 private String bxuanxiang; //B选项 9 private String cxuanxiang; //C选项 10 private String dxuanxiang; //D选项 11 private String daan; //答案 12 private String jiexi; //解析 13 public String getTimu() { 14 return timu; 15 } 16 public void setTimu(String timu) { 17 this.timu = timu; 18 } 19 public String getLeixing() { 20 return leixing; 21 } 22 public void setLeixing(String leixing) { 23 this.leixing = leixing; 24 } 25 26 public String getAxuanxiang() { 27 return axuanxiang; 28 } 29 public void setAxuanxiang(String axuanxiang) { 30 this.axuanxiang = axuanxiang; 31 } 32 public String getBxuanxiang() { 33 return bxuanxiang; 34 } 35 public void setBxuanxiang(String bxuanxiang) { 36 this.bxuanxiang = bxuanxiang; 37 } 38 public String getCxuanxiang() { 39 return cxuanxiang; 40 } 41 public void setCxuanxiang(String cxuanxiang) { 42 this.cxuanxiang = cxuanxiang; 43 } 44 public String getDxuanxiang() { 45 return dxuanxiang; 46 } 47 public void setDxuanxiang(String dxuanxiang) { 48 this.dxuanxiang = dxuanxiang; 49 } 50 public String getDaan() { 51 return daan; 52 } 53 public void setDaan(String daan) { 54 this.daan = daan; 55 } 56 public String getJiexi() { 57 return jiexi; 58 } 59 public void setJiexi(String jiexi) { 60 this.jiexi = jiexi; 61 } 62 @Override 63 public String toString() { 64 return "Question [timu=" + timu + ", leixing=" + leixing + ", axuanxiang=" + axuanxiang + ", bxuanxiang=" 65 + bxuanxiang + ", cxuanxiang=" + cxuanxiang + ", dxuanxiang=" + dxuanxiang + ", daan=" + daan 66 + ", jiexi=" + jiexi + "]"; 67 } 68 public Question(String timu, String leixing, String axuanxiang, String bxuanxiang, String cxuanxiang, 69 String dxuanxiang, String daan, String jiexi) { 70 super(); 71 this.timu = timu; 72 this.leixing = leixing; 73 this.axuanxiang = axuanxiang; 74 this.bxuanxiang = bxuanxiang; 75 this.cxuanxiang = cxuanxiang; 76 this.dxuanxiang = dxuanxiang; 77 this.daan = daan; 78 this.jiexi = jiexi; 79 } 80 public Question() { 81 super(); 82 } 83 84 85 86 }
导出Java
package Action.Excel2; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.eclipse.jdt.internal.compiler.ast.ThisReference; import org.jdom.Attribute; import org.jdom.Element; import bean.Question; public class GenerateExcelPaper { /** * 产生Excel试卷 *@param list 题目集合 */ public static void exportExcelPaper(List<Question> list) { // 标题 String[] title = { "题目", "类型", "A选项", "B选项", "C选项", "D选项", "答案", "解析" }; // 创建一个工作簿 HSSFWorkbook workbook = new HSSFWorkbook(); // 创建一个工作表sheet HSSFSheet sheet = workbook.createSheet(); // 设置列宽 setColumnWidth(sheet, 8); // 创建第一行 HSSFRow row = sheet.createRow(0); // 创建一个单元格 HSSFCell cell = null; // 创建表头 for (int i = 0; i < title.length; i++) { cell = row.createCell(i); // 设置样式 HSSFCellStyle cellStyle = workbook.createCellStyle(); cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 设置字体居中 // 设置字体 HSSFFont font = workbook.createFont(); font.setFontName("宋体"); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 字体加粗 // font.setFontHeight((short)12); font.setFontHeightInPoints((short) 13); cellStyle.setFont(font); cell.setCellStyle(cellStyle); cell.setCellValue(title[i]); } // 模拟数据 /*List<Question> list = new ArrayList<>(); list.add(new Question("你喜欢吃什么", "单选", "老司机", "方便吗", "辣条", "牛奶", "老司机", "我是老司机")); list.add(new Question("你喜欢吃什么", "单选", "老司机", "方便吗", "辣条", "牛奶", "老司机", "我是老司机")); list.add(new Question("你喜欢吃什么", "单选", "老司机", "方便吗", "辣条", "牛奶", "老司机", "我是老司机")); list.add(new Question("你喜欢吃什么", "单选", "老司机", "方便吗", "辣条", "牛奶", "老司机", "我是老司机")); list.add(new Question("你是人", "判断", "", "", "", "", "是", "我是老司机")); list.add(new Question("你是人", "判断", "", "", "", "", "是", "我是老司机"));*/ System.out.println(list); // 从第二行开始追加数据 for (int i = 1; i < (list.size() + 1); i++) { // 创建第i行 HSSFRow nextRow = sheet.createRow(i); for (int j = 0; j < 8; j++) { Question eQuestion = list.get(i-1); HSSFCell cell2 = nextRow.createCell(j); if (j == 0) { cell2.setCellValue( eQuestion.getTimu()); } if (j == 1) { cell2.setCellValue(eQuestion.getLeixing()); } if (j == 2) { cell2.setCellValue(eQuestion.getAxuanxiang()); } if (j == 3) { cell2.setCellValue(eQuestion.getBxuanxiang()); } if (j == 4) { cell2.setCellValue(eQuestion.getCxuanxiang()); } if (j == 5) { cell2.setCellValue(eQuestion.getDxuanxiang()); } if (j == 6) { cell2.setCellValue(eQuestion.getDaan()); } if (j == 7) { cell2.setCellValue(eQuestion.getJiexi()); } } } // 创建一个文件 File file = new File("E:/1.xls"); try { file.createNewFile(); // 打开文件流 FileOutputStream outputStream = FileUtils.openOutputStream(file); workbook.write(outputStream); outputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 设置列宽() private static void setColumnWidth(HSSFSheet sheet, int colNum) { for (int i = 0; i < colNum; i++) { int v = 0; v = Math.round(Float.parseFloat("15.0") * 37F); v = Math.round(Float.parseFloat("20.0") * 267.5F); sheet.setColumnWidth(i, v); } } }
测试:
Java将list数据导出到Excel——(八)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。