1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| @SpringBootTest public class POITest {
public static void write() { XSSFWorkbook excel = new XSSFWorkbook(); XSSFSheet sheetInfo1 = excel.createSheet("info1"); XSSFRow row = sheetInfo1.createRow(0); row.createCell(0).setCellValue("姓名-这是第1行第1格"); row.createCell(2).setCellValue("城市-这是第1行第3格");
XSSFRow row2 = sheetInfo1.createRow(2); row2.createCell(0).setCellValue("三儿"); row2.createCell(2).setCellValue("北京");
FileOutputStream out = null; try { out = new FileOutputStream(new File("G:\\test.xlsx")); excel.write(out); } catch (IOException e) { throw new RuntimeException(e); } finally { try { out.close(); excel.close(); } catch (IOException e) { throw new RuntimeException(e); } } }
public static void read() { XSSFWorkbook sheets = null; FileInputStream is = null; try { is = new FileInputStream(new File("G:\\test.xlsx")); sheets = new XSSFWorkbook(is); XSSFSheet sheetAt = sheets.getSheetAt(0); int lastRowNum = sheetAt.getLastRowNum(); for (int i = 0; i <= lastRowNum; i++) { XSSFRow row = sheetAt.getRow(i); if (row == null){ row = sheetAt.createRow(i); } int lastCellNum = row.getLastCellNum(); for (int j = 0; j < lastCellNum; j++) { XSSFCell cell = row.getCell(j); if (cell != null) System.out.print(cell.getStringCellValue()); } System.out.println(); }
} catch (IOException e) { throw new RuntimeException(e); } finally { try { is.close(); sheets.close(); } catch (IOException e) { throw new RuntimeException(e); } } }
public static void main(String[] args) { write(); read();
} }
|