代码:使用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对象中?
答案 0 :(得分:0)
尝试这种方式添加值DTO
使用Id,Name,Location,Role,Salary
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("");
}