java使用poi把從數據庫中取出的數據寫入到excel文件中並保存到指定文件路徑


  有時候我們要把從數據庫中取出的數據導入到excel中,使取到的數據看起來更加的直觀和方便,在java中如何實現取到的數據導入到excel中呢?以下就是使用poi工具吧數據寫入excel文件中的解決方法:
Excel表格擴展名有.xlsx和.xls兩種格式
       百度上對兩種文件的介紹有很多就不一一列舉,基本的不同總結下來有以下幾點:


 

  在java中讀取和寫入.xls格式使用maven導入jar包: 
 
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>

 

  在java中讀取和寫入.xlsx格式使用maven導入jar包:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.16</version>
</dependency>

 

  然后就可以使用jar包讀取excel文件,並保存到本地指定的位置,首先把從數據庫中取出的信息放到一個list中,然后從list中一一讀取數據,寫入到excel文件中,由於后面還有需求約定好使用.xlsx文件,這里生成的excel文件類型便是.xlsx文件,如果需求對文件類型沒有要求,盡量生成.xls文件。
/**
*
* @param stuList 從數據庫中查詢需要導入excel文件的信息列表
* @return 返回生成的excel文件的路徑
* @throws Exception
*/
public static String stuList2Excel(List<Student> stuList) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd hhmmss");
Workbook wb = new XSSFWorkbook();
//標題行抽出字段
String[] title = {"序號","學號", "姓名", "性別", "入學時間", "住址", "手機號", "其他信息"};
//設置sheet名稱,並創建新的sheet對象
String sheetName = "學生信息一覽";
Sheet stuSheet = wb.createSheet(sheetName);
//獲取表頭行
Row titleRow = stuSheet.createRow(0);
//創建單元格,設置style居中,字體,單元格大小等
CellStyle style = wb.createCellStyle();
Cell cell = null;
//把已經寫好的標題行寫入excel文件中
for (int i = 0; i < title.length; i++) {
cell = titleRow.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(style);
}
//把從數據庫中取得的數據一一寫入excel文件中
Row row = null;
for (int i = 0; i < stuList.size(); i++) {
//創建list.size()行數據
row = stuSheet.createRow(i + 1);
//把值一一寫進單元格里
//設置第一列為自動遞增的序號
row.createCell(0).setCellValue(i + 1);
row.createCell(1).setCellValue(stuList.get(i).getStuId());
row.createCell(2).setCellValue(stuList.get(i).getStuName());
row.createCell(3).setCellValue(stuList.get(i).getGender());
//把時間轉換為指定格式的字符串再寫入excel文件中
if (stuList.get(i).getEnterTime() != null) {
row.createCell(4).setCellValue(sdf.format(stuList.get(i).getEnterTime()));
}
row.createCell(5).setCellValue(stuList.get(i).getAddress());
row.createCell(6).setCellValue(stuList.get(i).getPhone());
row.createCell(7).setCellValue(stuList.get(i).getOtherInfo());

}
//設置單元格寬度自適應,在此基礎上把寬度調至1.5倍
for (int i = 0; i < title.length; i++) {
stuSheet.autoSizeColumn(i, true);
stuSheet.setColumnWidth(i, stuSheet.getColumnWidth(i) * 15 / 10);
}
//獲取配置文件中保存對應excel文件的路徑,本地也可以直接寫成F:excel/stuInfoExcel路徑
String folderPath = ResourceBundle.getBundle("systemconfig").getString("downloadFolder") + File.separator + "stuInfoExcel";

//創建上傳文件目錄
File folder = new File(folderPath);
//如果文件夾不存在創建對應的文件夾
if (!folder.exists()) {
folder.mkdirs();
}
//設置文件名
String fileName = sdf1.format(new Date()) + sheetName + ".xlsx";
String savePath = folderPath + File.separator + fileName;
// System.out.println(savePath);

OutputStream fileOut = new FileOutputStream(savePath);
wb.write(fileOut);
fileOut.close();
//返回文件保存全路徑
return savePath;
}

   注意事項:

  1. 這里的數據使用的是數據庫中的測試數據,生產環境數據字段會更多,數據會更復雜,要根據不同的數據進行處理。
  2. poi工具對生成的單元格寬度即使設置了自適應,有時寬度也無法顯示全部數據,所以在此基礎上,根據情況把單元格寬度再增加一些,這里是把單元格寬度再增加1.5倍。
  3. 文件保存在同一個文件夾中會導致重名,所以文件名中最好包含時間,這樣的話在單人使用的時候回避免出現文件名重復的情況。

  根據返回的路徑名手動找到的文件:

 

  得到的excel數據如下

 

   以上就是從數據庫中讀取數據寫入excel文件並保存到指定位置,后續還有前端請求下載excel文件,后端的處理方法;前端上傳excel文件,讀取其中的文件信息,並把對應的數據取出存入到數據庫中......

  未完待續。。。。。。

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM