java 下载excel xlsx_JAVA Excel.xlsx 上传于下载
1.文件下載
// 下載文件名
String systemDate = DateUtil.getDate(DateUtil.FORMAT_DATETIME);
String fileName = "StoProcessHistory_" + systemDate + Constants.RESOURCE_TEMPLAT_FILE_TYPE;
// 獲取根路徑
String url = this.getClass().getResource(Constants.STRING_EMPTY).getPath();
int indexLen = url.indexOf(Constants.RESOURCE_TEMPLAT_FILE_NAME);
String rootPath = url.substring(0, indexLen);
// 獲取模板位置,讀取數據庫(也可以讀取配置文件或寫死)
String templatePath = rootPath + Constants.RESOURCE_TEMPLAT_PATH;
// 實際位置
String path = templatePath + templateName;
// 給服務器上的EXCEL下拉框賦值
InputStream is = new FileInputStream(path);
XSSFWorkbook workBook = new XSSFWorkbook(is);
XSSFSheet sheet = workBook.getSheetAt(0);
// 從數據庫中取出數據
List resultListExcel = stockSearchListService.getStockSearchList(searchDto, null);
// 數據存在的情況下
if (resultListExcel != null && resultListExcel.size() != 0) {
// 向excel文件中寫內容
sheet = stockSearchListService.setSelectDataToExcel(sheet, resultListExcel, outputFlag);
// 設置文件ContentType類型,這樣設置,會自動判斷下載文件類型
response.setContentType("multipart/form-data");
// 設置文件頭:最后一個參數是設置下載文件名
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
ServletOutputStream out = response.getOutputStream();
workBook.write(out);
out.close();
return null;
} else {
// 0件的場合
this.addActionMessage(getText(MsgCodeConstants.COMMON_MESSAGE_NORESULT));
return SUCCESS;
}
/**
* 向excel文件中寫內容
*
* @param oldSheet XSSFSheet
* @param dataList List
* @return 處理結果
*/
@Override
public XSSFSheet setSelectDataToExcel(XSSFSheet oldSheet, List stoSearchDtoList, String outputFlag)
throws Exception {
CellStyle style0 = oldSheet.getRow(1).getCell(0).getCellStyle();
for (int i = 0; i < stoSearchDtoList.size(); i++) {
WmsStoStockDto dtoL = stoSearchDtoList.get(i);
XSSFRow rowLine = oldSheet.createRow(i + 1);
Cell cell = null;
// 編號
cell = rowLine.createCell(0);
cell.setCellStyle(style0);
cell.setCellValue(i + 1);
}
return oldSheet;
}
2.文件上傳
/**
* 讀取excel文件
*
* @param path 文件路徑
* @return 處理結果
*/
@Override
public List getExcelFile(String path, File uploadFile) throws Exception {
// 路徑編碼的轉換
byte[] b = path.getBytes("ISO-8859-1");
String strPath = new String(b, "utf-8");
XSSFWorkbook wb = this.readExcelUtils(strPath, uploadFile);
if (wb == null) {
return null;
}
XSSFSheet sheet = wb.getSheetAt(0);
int rowNum = sheet.getLastRowNum();
// 讀取的數據list
List dataList = new ArrayList();
// 正文內容應該從第二行開始,第一行為表頭的標題
for (int i = 10; i <= rowNum; i += 2) {
XSSFRow row = sheet.getRow(i);
XSSFRow row2 = sheet.getRow(i + 1);
WmsStoCheckDetailDto lineDto = new WmsStoCheckDetailDto();
XSSFRow firstRow = sheet.getRow(0);
// 盤點編號
String item0 = firstRow.getCell(91).getRichStringCellValue().getString().replace("盤點編號:", "");
lineDto.setStockCheckId(item0);
// 盤點明細編號
String item1 = row.getCell(0).getRichStringCellValue().getString();
lineDto.setCheckDetailNo(item1);
// 盤點數量
String item2 = row2.getCell(54).getRichStringCellValue().getString();
lineDto.setCheckNum(item2);
// 盤點擔當
String item3 = row2.getCell(81).getRichStringCellValue().getString();
lineDto.setCheckUserName(item3);
// excel對應的行號
lineDto.setLineNo(i + 1);
// 空白行跳過
if (ValidatorUtil.isEmptyIgnoreSpace(item0) && ValidatorUtil.isEmptyIgnoreSpace(item1)
&& ValidatorUtil.isEmptyIgnoreSpace(item2) && ValidatorUtil.isEmptyIgnoreSpace(item3)) {
continue;
}
dataList.add(lineDto);
}
return dataList;
}
總結
以上是生活随笔為你收集整理的java 下载excel xlsx_JAVA Excel.xlsx 上传于下载的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java符号引用 直接引用_JVM的符号
- 下一篇: java线程唤醒线程_Java中如何唤醒