【问题标题】:how to store a parsed .xslx file in a dto如何将解析的 .xlsx 文件存储在 dto 中
【发布时间】:2013-05-03 11:42:51
【问题描述】:

代码:使用 poi 解析 excel 文件并在控制台中打印输出,并创建一个新的 excel 文件来显示输出。

XSSFWorkbook workbook = new XSSFWorkbook(fileName);
XSSFSheet sheet = workbook.getSheetAt(0);
    XSSFRow row ;
    XSSFCell cell;


    Iterator<Row>  rows = sheet.rowIterator();

    while(rows.hasNext())
    {
        row = (XSSFRow)rows.next();
        Iterator<Cell> cells = row.cellIterator();
        while(cells.hasNext())
        {
            cell = (XSSFCell)cells.next();

            switch(cell.getCellType()) 
            {
            case Cell.CELL_TYPE_BOOLEAN: System.out.println(cell.getBooleanCellValue()+"\t\t");
            break;
            case Cell.CELL_TYPE_NUMERIC: System.out.println(cell.getNumericCellValue()+ "\t\t");
            break;
            case Cell.CELL_TYPE_STRING:System.out.println(cell.getStringCellValue()+ "\t\t");
            break;
            }

        }System.out.println("");
    }fileName.close();  

    FileOutputStream out = new FileOutputStream(new File("C://data.xlsx"));
    workbook.write(out);
    out.close();

输出: 身份证
名称
位置
角色
工资

111.0
库马尔
钦奈
开发商
1000.0

112.0
拉森
班加罗尔
开发商
2000.0

查询: 1.如何得到与excel相同格式的输出? 2. 如何将输出存储在 DTO 对象中?

【问题讨论】:

    标签: java excel dto


    【解决方案1】:

    尝试这种方式添加Values DTO

    使用Id,Name,Location,Role,Salary 等属性创建一个Student DTO 和setter 和getter

         while(rows.hasNext())
                {
                    row = (XSSFRow)rows.next();
                    Iterator<Cell> cells = row.cellIterator();
        StudentDTO std = new StudentDTO();
                    while(cells.hasNext())
                    {
                        cell = (XSSFCell)cells.next();
    
                        switch(cell.getCellType()) 
                        {
                        case Cell.CELL_TYPE_BOOLEAN: System.out.println(cell.getBooleanCellValue()+"\t\t");
        std.setId(cell.getBooleanCellValue());
                        break;
                        case Cell.CELL_TYPE_NUMERIC: System.out.println(cell.getNumericCellValue()+ "\t\t");
        std.setName(cell.getNumericCellValue());
                        break;
                        case Cell.CELL_TYPE_STRING:System.out.println(cell.getStringCellValue()+ "\t\t");
    std.setLocation(cell.getStringCellValue());
                        break;
                        }
    
                    }System.out.println("");
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-23
      • 1970-01-01
      • 2013-05-30
      • 2021-09-20
      • 1970-01-01
      • 2015-08-01
      • 1970-01-01
      相关资源
      最近更新 更多