如何在dto中存储解析.xlsx文件

时间:2013-05-03 11:42:52

标签: java excel dto

代码:使用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对象中?

1 个答案:

答案 0 :(得分:0)

尝试这种方式添加值DTO

使用Id,Name,Location,Role,Salary

等属性创建一个学生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("");
            }