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);
// 獲取模板位置,讀取數(shù)據(jù)庫(kù)(也可以讀取配置文件或?qū)懰?
String templatePath = rootPath + Constants.RESOURCE_TEMPLAT_PATH;
// 實(shí)際位置
String path = templatePath + templateName;
// 給服務(wù)器上的EXCEL下拉框賦值
InputStream is = new FileInputStream(path);
XSSFWorkbook workBook = new XSSFWorkbook(is);
XSSFSheet sheet = workBook.getSheetAt(0);
// 從數(shù)據(jù)庫(kù)中取出數(shù)據(jù)
List resultListExcel = stockSearchListService.getStockSearchList(searchDto, null);
// 數(shù)據(jù)存在的情況下
if (resultListExcel != null && resultListExcel.size() != 0) {
// 向excel文件中寫(xiě)內(nèi)容
sheet = stockSearchListService.setSelectDataToExcel(sheet, resultListExcel, outputFlag);
// 設(shè)置文件ContentType類(lèi)型,這樣設(shè)置,會(huì)自動(dòng)判斷下載文件類(lèi)型
response.setContentType("multipart/form-data");
// 設(shè)置文件頭:最后一個(gè)參數(shù)是設(shè)置下載文件名
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
ServletOutputStream out = response.getOutputStream();
workBook.write(out);
out.close();
return null;
} else {
// 0件的場(chǎng)合
this.addActionMessage(getText(MsgCodeConstants.COMMON_MESSAGE_NORESULT));
return SUCCESS;
}
/**
* 向excel文件中寫(xiě)內(nèi)容
*
* @param oldSheet XSSFSheet
* @param dataList List
* @return 處理結(jié)果
*/
@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;
// 編號(hào)
cell = rowLine.createCell(0);
cell.setCellStyle(style0);
cell.setCellValue(i + 1);
}
return oldSheet;
}
2.文件上傳
/**
* 讀取excel文件
*
* @param path 文件路徑
* @return 處理結(jié)果
*/
@Override
public List getExcelFile(String path, File uploadFile) throws Exception {
// 路徑編碼的轉(zhuǎn)換
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();
// 讀取的數(shù)據(jù)list
List dataList = new ArrayList();
// 正文內(nèi)容應(yīng)該從第二行開(kāi)始,第一行為表頭的標(biāo)題
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);
// 盤(pán)點(diǎn)編號(hào)
String item0 = firstRow.getCell(91).getRichStringCellValue().getString().replace("盤(pán)點(diǎn)編號(hào):", "");
lineDto.setStockCheckId(item0);
// 盤(pán)點(diǎn)明細(xì)編號(hào)
String item1 = row.getCell(0).getRichStringCellValue().getString();
lineDto.setCheckDetailNo(item1);
// 盤(pán)點(diǎn)數(shù)量
String item2 = row2.getCell(54).getRichStringCellValue().getString();
lineDto.setCheckNum(item2);
// 盤(pán)點(diǎn)擔(dān)當(dāng)
String item3 = row2.getCell(81).getRichStringCellValue().getString();
lineDto.setCheckUserName(item3);
// excel對(duì)應(yīng)的行號(hào)
lineDto.setLineNo(i + 1);
// 空白行跳過(guò)
if (ValidatorUtil.isEmptyIgnoreSpace(item0) && ValidatorUtil.isEmptyIgnoreSpace(item1)
&& ValidatorUtil.isEmptyIgnoreSpace(item2) && ValidatorUtil.isEmptyIgnoreSpace(item3)) {
continue;
}
dataList.add(lineDto);
}
return dataList;
}
總結(jié)
以上是生活随笔為你收集整理的java 下载excel xlsx_JAVA Excel.xlsx 上传于下载的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
                            
                        - 上一篇: java符号引用 直接引用_JVM的符号
 - 下一篇: java线程唤醒线程_Java中如何唤醒