ITeye博客

poi写入Excel

vergilwang · 2015-02-25 16:56 · Java · 编程语言 · Markdown

上一篇讲解了一下如何读取excel,那在这篇就讲一下如何去写文件吧! 同样是用poi类工具去实现的。

 

Java代码  收藏代码

  1. package com.common.util;
  2. import java.util.List;
  3. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  4. import org.apache.poi.ss.usermodel.Cell;
  5. import org.apache.poi.ss.usermodel.CellStyle;
  6. import org.apache.poi.ss.usermodel.Font;
  7. import org.apache.poi.ss.usermodel.Row;
  8. import org.apache.poi.ss.usermodel.Sheet;
  9. import org.apache.poi.ss.usermodel.Workbook;
  10. import org.apache.poi.ss.util.CellRangeAddress;
  11. import org.apache.poi.xssf.usermodel.XSSFCellStyle;
  12. public class ExcelUtil{
  13. public static HSSFWorkbook exportExcelForStudent(List studentList
  14. ) {     //创建excel文件对象
  15. HSSFWorkbook wb = new HSSFWorkbook();
  16. //创建一个张表
  17. Sheet sheet = wb.createSheet();
  18. //创建第一行
  19. Row row = sheet.createRow(0);
  20. //创建第二行
  21. Row row1 = sheet.createRow(1);
  22. // 文件头字体
  23. Font font0 = createFonts(wb, Font.BOLDWEIGHT_BOLD, "宋体", false,
  24. (short) 200);
  25. Font font1 = createFonts(wb, Font.BOLDWEIGHT_NORMAL, "宋体", false,
  26. (short) 200);
  27. // 合并第一行的单元格
  28. sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 1));
  29. //设置第一列的文字
  30. createCell(wb, row, 0, “总数”, font0);
  31. //合并第一行的2列以后到8列(不包含第二列)
  32. sheet.addMergedRegion(new CellRangeAddress(0, 0, 2, 8));
  33. //设置第二列的文字
  34. createCell(wb, row, 2, “基本信息”, font0);
  35. //给第二行添加文本
  36. createCell(wb, row1, 0, "序号", font1);
  37. createCell(wb, row1, 1, "版本", font1);
  38. createCell(wb, row1, 2, "姓名", font1);
  39. createCell(wb, row1, 3, "性别", font1);
  40. createCell(wb, row1, 4, "年龄", font1);
  41. createCell(wb, row1, 5, "年级", font1);
  42. createCell(wb, row1, 6, "学校", font1);
  43. createCell(wb, row1, 7, "父母名称", font1);
  44. createCell(wb, row1, 8, "籍贯", font1);
  45. createCell(wb, row1, 9, "联系方式", font1);
  46. //第三行表示
  47. int l = 2;
  48. //这里将学员的信心存入到表格中
  49. for (int i = 0; i < studentList.size(); i++) {
  50. //创建一行
  51. Row rowData = sheet.createRow(l++);
  52. Student stu = studentList.get(i);
  53. createCell(wb, rowData, 0, String.valueOf(i + 1), font1);
  54. createCell(wb, rowData, 1, "3.0", font1);
  55. createCell(wb, rowData, 2, stu.getName(), font1);
  56. createCell(wb, rowData, 3, stu.getStudentsex(), font1);
  57. createCell(wb, rowData, 4, stu.getStudentage(), font1);
  58. createCell(wb, rowData, 5, stu.getGrade().getName(), font1);
  59. createCell(wb, rowData, 6, stu.getStudentschool(), font1);
  60. createCell(wb, rowData, 7, stu.getparents(), font1);
  61. createCell(wb, rowData, 8, stu.getStudentprovince()+stu.getStudentcity()+stu.getStudentarea(), font1);
  62. createCell(wb, rowData, 9, stu.getContact(), font1);
  63. }
  64. return wb;
  65. }
  66. /**
  67. * 创建单元格并设置样式,值
  68. *
  69. * @param wb
  70. * @param row
  71. * @param column
  72. * @param
  73. * @param
  74. * @param value
  75. */
  76. public static void createCell(Workbook wb, Row row, int column,
  77. String value, Font font) {
  78. Cell cell = row.createCell(column);
  79. cell.setCellValue(value);
  80. CellStyle cellStyle = wb.createCellStyle();
  81. cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
  82. cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_BOTTOM);
  83. cellStyle.setFont(font);
  84. cell.setCellStyle(cellStyle);
  85. }
  86. /**
  87. * 设置字体
  88. *
  89. * @param wb
  90. * @return
  91. */
  92. public static Font createFonts(Workbook wb, short bold, String fontName,
  93. boolean isItalic, short hight) {
  94. Font font = wb.createFont();
  95. font.setFontName(fontName);
  96. font.setBoldweight(bold);
  97. font.setItalic(isItalic);
  98. font.setFontHeight(hight);
  99. return font;
  100. }
  101. /**
  102. * 判断是否为数字
  103. *
  104. * @param str
  105. * @return
  106. */
  107. public static boolean isNumeric(String str) {
  108. if (str == null || "".equals(str.trim()) || str.length() > 10)
  109. return false;
  110. Pattern pattern = Pattern.compile("^0|[1-9]\\d*(\\.\\d+)?$");
  111. return pattern.matcher(str).matches();
  112. }
  113. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。