生活随笔
收集整理的這篇文章主要介紹了
poi写入Excel
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
上一篇講解了一下如何讀取excel,那在這篇就講一下如何去寫文件吧!?同樣是用poi類工具去實現(xiàn)的。
?
Java代碼??
package?com.common.util;????import?java.util.List;????import?org.apache.poi.hssf.usermodel.HSSFWorkbook;??import?org.apache.poi.ss.usermodel.Cell;??import?org.apache.poi.ss.usermodel.CellStyle;??import?org.apache.poi.ss.usermodel.Font;??import?org.apache.poi.ss.usermodel.Row;??import?org.apache.poi.ss.usermodel.Sheet;??import?org.apache.poi.ss.usermodel.Workbook;??import?org.apache.poi.ss.util.CellRangeAddress;??import?org.apache.poi.xssf.usermodel.XSSFCellStyle;????public?class?ExcelUtil{????????public?static?HSSFWorkbook?exportExcelForStudent(List?studentList??)?{???????????????HSSFWorkbook?wb?=?new?HSSFWorkbook();????????????????????Sheet?sheet?=?wb.createSheet();????????????????????Row?row?=?sheet.createRow(0);???????????????????????????????Row?row1?=?sheet.createRow(1);????????????????????Font?font0?=?createFonts(wb,?Font.BOLDWEIGHT_BOLD,?"宋體",?false,??????????????????(short)?200);??????????Font?font1?=?createFonts(wb,?Font.BOLDWEIGHT_NORMAL,?"宋體",?false,??????????????????(short)?200);????????????????????sheet.addMergedRegion(new?CellRangeAddress(0,?0,?0,?1));????????????????????createCell(wb,?row,?0,?“總數(shù)”,?font0);????????????????????sheet.addMergedRegion(new?CellRangeAddress(0,?0,?2,?8));????????????????????createCell(wb,?row,?2,?“基本信息”,?font0);????????????????????createCell(wb,?row1,?0,?"序號",?font1);??????????createCell(wb,?row1,?1,?"版本",?font1);??????????createCell(wb,?row1,?2,?"姓名",?font1);??????????createCell(wb,?row1,?3,?"性別",?font1);??????????createCell(wb,?row1,?4,?"年齡",?font1);??????????createCell(wb,?row1,?5,?"年級",?font1);??????????createCell(wb,?row1,?6,?"學(xué)校",?font1);??????????createCell(wb,?row1,?7,?"父母名稱",?font1);??????????createCell(wb,?row1,?8,?"籍貫",?font1);??????????createCell(wb,?row1,?9,?"聯(lián)系方式",?font1);????????????????????int?l?=?2;????????????????????for?(int?i?=?0;?i?<?studentList.size();?i++)?{????????????????????????????Row?rowData?=?sheet.createRow(l++);??????????????Student?stu?=?studentList.get(i);??????????????createCell(wb,?rowData,?0,?String.valueOf(i?+?1),?font1);??????????????createCell(wb,?rowData,?1,?"3.0",?font1);??????????????createCell(wb,?rowData,?2,?stu.getName(),?font1);??????????????createCell(wb,?rowData,?3,?stu.getStudentsex(),?font1);???????????????createCell(wb,?rowData,?4,?stu.getStudentage(),?font1);??????????????createCell(wb,?rowData,?5,?stu.getGrade().getName(),?font1);??????????????createCell(wb,?rowData,?6,?stu.getStudentschool(),?font1);??????????????createCell(wb,?rowData,?7,?stu.getparents(),?font1);???????????????createCell(wb,?rowData,?8,?stu.getStudentprovince()+stu.getStudentcity()+stu.getStudentarea(),?font1);??????????????createCell(wb,?rowData,?9,?stu.getContact(),?font1);????????????????}??????????return?wb;??????}??????????????????????public?static?void?createCell(Workbook?wb,?Row?row,?int?column,??????????????String?value,?Font?font)?{??????????Cell?cell?=?row.createCell(column);??????????cell.setCellValue(value);??????????CellStyle?cellStyle?=?wb.createCellStyle();??????????cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);??????????cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_BOTTOM);??????????cellStyle.setFont(font);??????????cell.setCellStyle(cellStyle);??????}???????????????????public?static?Font?createFonts(Workbook?wb,?short?bold,?String?fontName,??????????????boolean?isItalic,?short?hight)?{??????????Font?font?=?wb.createFont();??????????font.setFontName(fontName);??????????font.setBoldweight(bold);??????????font.setItalic(isItalic);??????????font.setFontHeight(hight);??????????return?font;??????}???????????????????public?static?boolean?isNumeric(String?str)?{??????????if?(str?==?null?||?"".equals(str.trim())?||?str.length()?>?10)??????????????return?false;??????????Pattern?pattern?=?Pattern.compile("^0|[1-9]\\d*(\\.\\d+)?$");??????????return?pattern.matcher(str).matches();??????}????}?? ?
總結(jié)
以上是生活随笔為你收集整理的poi写入Excel的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。