日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

poi写入Excel

發布時間:2025/4/14 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 poi写入Excel 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

上一篇講解了一下如何讀取excel,那在這篇就講一下如何去寫文件吧!?同樣是用poi類工具去實現的。

?

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??
  • )?{?????//創建excel文件對象??
  • ????????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,?“總數”,?font0);??
  • ????????//合并第一行的2列以后到8列(不包含第二列)??
  • ????????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,?"學校",?font1);??
  • ????????createCell(wb,?row1,?7,?"父母名稱",?font1);??
  • ????????createCell(wb,?row1,?8,?"籍貫",?font1);??
  • ????????createCell(wb,?row1,?9,?"聯系方式",?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;??
  • ????}?????
  • ??
  • /**?
  • ?????*?創建單元格并設置樣式,值?
  • ?????*??
  • ?????*?@param?wb?
  • ?????*?@param?row?
  • ?????*?@param?column?
  • ?????*?@param?
  • ?????*?@param?
  • ?????*?@param?value?
  • ?????*/??
  • ????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);??
  • ????}??
  • ??
  • ????/**?
  • ?????*?設置字體?
  • ?????*??
  • ?????*?@param?wb?
  • ?????*?@return?
  • ?????*/??
  • ????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;??
  • ????}??
  • ??
  • ????/**?
  • ?????*?判斷是否為數字?
  • ?????*??
  • ?????*?@param?str?
  • ?????*?@return?
  • ?????*/??
  • ????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();??
  • ????}??
  • ??
  • }??
  • ?

    總結

    以上是生活随笔為你收集整理的poi写入Excel的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。