@Test
public void test1() throws Exception {
FileOutputStream fos = new FileOutputStream("C:\\Users\\suqianlong\\Desktop\\test\\b.xlsx");
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet();
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("姓名");
Cell cell1 = row.createCell(1);
cell1.setCellValue("班级");
Cell cell2 = row.createCell(2);
cell2.setCellValue("学生成绩表");
Row row1 = sheet.createRow(1);
Cell cell3 = row1.createCell(2);
cell3.setCellValue("第一学期");
Cell cell4 = row1.createCell(3);
cell4.setCellValue("第二学期");
// 合并单元格
CellRangeAddress cra1 = new CellRangeAddress(0,1,0,0);
CellRangeAddress cra2 = new CellRangeAddress(0,1,1,1);
CellRangeAddress cra = new CellRangeAddress(0,0,2,3);
sheet.addMergedRegion(cra1);
sheet.addMergedRegion(cra2);
sheet.addMergedRegion(cra);
wb.write(fos);
fos.close();
}
@Test
public void test2() throws Exception {
FileOutputStream fos = new FileOutputStream("C:\\Users\\suqianlong\\Desktop\\test\\c.xlsx");
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet();
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("姓名");
Cell cell1 = row.createCell(1);
cell1.setCellValue("班级");
Cell cell2 = row.createCell(2);
cell2.setCellValue("学生成绩表");
Row row1 = sheet.createRow(1);
Cell cell3 = row1.createCell(2);
cell3.setCellValue("第一学期");
Cell cell4 = row1.createCell(4);
cell4.setCellValue("第二学期");
Row row2 = sheet.createRow(2);
Cell cell5 = row2.createCell(2);
cell5.setCellValue("前半年");
Cell cell6 = row2.createCell(3);
cell6.setCellValue("后半年");
Cell cell7 = row2.createCell(4);
cell7.setCellValue("前半年");
Cell cell8 = row2.createCell(5);
cell8.setCellValue("后半年");
// 合并单元格
CellRangeAddress cra1 = new CellRangeAddress(0,2,0,0);
CellRangeAddress cra2 = new CellRangeAddress(0,2,1,1);
CellRangeAddress cra = new CellRangeAddress(0,0,2,5);
CellRangeAddress cra3 = new CellRangeAddress(1,1,2,3);
CellRangeAddress cra4 = new CellRangeAddress(1,1,4,5);
sheet.addMergedRegion(cra1);
sheet.addMergedRegion(cra2);
sheet.addMergedRegion(cra);
sheet.addMergedRegion(cra3);
sheet.addMergedRegion(cra4);
wb.write(fos);
fos.close();
}
读操作
高性能读使用 Workbook wb = new SXSSFWorkbook();
@Test
public void testImport() throws Exception {
String PATH = "H:\\LUA\\redis_springboot\\src\\main\\java\\com\\distributeredis\\redis_springboot\\springpoi\\明细表.xlsx";
FileInputStream fis = new FileInputStream(PATH);
// 获取一个工作簿
XSSFWorkbook workbook = new XSSFWorkbook(fis);
// 获取一个工作表
XSSFSheet sheet = workbook.getSheetAt(0);
// 获取第一行内容
XSSFRow row = sheet.getRow(0);
if(row != null) {
// 获取所有列
int numberOfCells = row.getPhysicalNumberOfCells();
for (int cell = 0; cell < numberOfCells; cell++) {
XSSFCell rowCell = row.getCell(cell);
if(rowCell != null) {
String cellValue = rowCell.getStringCellValue();
System.out.print(cellValue + "| ");
}
}
}
System.out.println();
// 获取表格内容
int rows = sheet.getPhysicalNumberOfRows();
// 从1开始,第一行是标题
for (int rowNum = 1; rowNum < rows; rowNum++) {
XSSFRow currentRow = sheet.getRow(rowNum);
if(currentRow != null) {
// 获取当前行的列数
int cells = currentRow.getPhysicalNumberOfCells();
for (int col = 0; col < cells; col++) {
// 获取当前列的值
XSSFCell cell = currentRow.getCell(col);
CellType cellType = cell.getCellType();
String cellValue = "";
switch (cellType) {
case STRING: // 字符串
cellValue = cell.getStringCellValue();
break;
case BOOLEAN: // 布尔类型
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
case BLANK: // 空
break;
case NUMERIC: // 数值类型(日期,普通数值)
if(DateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
cellValue = new DateTime(date).toString("yyyy-MM-dd");
}else {
cell.setCellType(CellType.STRING);
cellValue = cell.toString();
}
break;
case ERROR:
break;
}
System.out.print(cellValue+" | ");
}
}
System.out.println();
}
workbook.close();
fis.close();
}