`

poi写入Excel

    博客分类:
  • Java
 
阅读更多

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

 

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

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics