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<WmsStoStockDto> 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<WmsMstStorageDto>
* @return 处理结果
*/
@Override
public XSSFSheet setSelectDataToExcel(XSSFSheet oldSheet, List<WmsStoStockDto> 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<WmsStoCheckDetailDto> 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<WmsStoCheckDetailDto> dataList = new ArrayList<WmsStoCheckDetailDto>();

    // 正文内容应该从第二行开始,第一行为表头的标题
    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;
}
posted @ 2020-12-04 10:34  雪亲王  阅读(146)  评论(0编辑  收藏  举报