java 文件导出Excel 文本形式转数字格式解决,字段是int导出需求是数字解决方案

问题:java字段是int类型等,然后导出时是文本,需要操作人员转换为数字格式. 应操作人员要求,导出的时候就是数字格式可以不用转换,方便操作等

java 文件导出Excel 文本形式转数字格式解决,字段是int导出需求是数字解决方案_第1张图片

java 后台处理过程:

这里我只处理表格的0,2,7,8列的格式转换

图:java 文件导出Excel 文本形式转数字格式解决,字段是int导出需求是数字解决方案_第2张图片

java 代码:
// 总行数
private int totalRows = 0;
// 总条数
private int totalCells = 0;
// 获取总行数
public int getTotalRows() {
    return totalRows;
}
// 获取总列数
public int getTotalCells() {
    return totalCells;
}
/////////////导出字符串转换成数字格式///////////////
///将导出前,已经生成的工作表获取到 workBook,获取第一个工作表0
Sheet sheet = workbook.getSheetAt(0);
// 得到Excel的行数
this.totalRows = sheet.getPhysicalNumberOfRows();
// 得到Excel的列数(前提是有行数)
if (totalRows > 1 && sheet.getRow(0) != null) {
    this.totalCells = sheet.getRow(0).getPhysicalNumberOfCells();
}
// 循环Excel行数 r=3///从第几行 开始需要转换的数据,这里是从第3行开始是导出的真实数据
for (int r = 3; r < totalRows; r++) {
    Row row = sheet.getRow(r);
    if (row == null) {
        continue;
    }
for (int c = 0; c < this.totalCells; c++) {/////遍历列,对列进行转换就可以了
    Cell cell = row.getCell(c);
    CellStyle cellStyle = workbook.createCellStyle();
    if (null != cell) {
        if (c==0){
            if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
                String id = String.valueOf(cell.getStringCellValue());
                row.getCell(0).setCellValue(Double.parseDouble(id));
            }
        }
        if (c==2){
            if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
                String number = String.valueOf(cell.getStringCellValue());
                row.getCell(2).setCellValue(Double.parseDouble(number));
            }
        }
        if (c==7){
            if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
                String pageViews = String.valueOf(cell.getStringCellValue());
                row.getCell(7).setCellValue(Double.parseDouble(pageViews));
            }
        }
        if (c==8){
            if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
                String userViews = String.valueOf(cell.getStringCellValue());
                row.getCell(8).setCellValue(Double.parseDouble(userViews));
            }
        }
        }
}
}

导出效果(结果):

java 文件导出Excel 文本形式转数字格式解决,字段是int导出需求是数字解决方案_第3张图片

 

 

你可能感兴趣的:(java导出)