Office 2007 and above
Prerequisites, import jar package
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version>
Create excel sheet
public class WriteExcel07 { @Test public void writeExcel() throws IOException{ // Create workbook XSSFWorkbook workBook = new XSSFWorkbook(); // Create worksheet XSSFSheet sheet = workBook.createSheet("helloWorld" ); // Create row XSSFRow row = sheet.createRow(0 ); // Create a cell, operate the third row and third column XSSFCell cell = row.createCell(0 , CellType.STRING); cell.setCellValue("helloWorld"); FileOutputStream outputStream = new FileOutputStream(new File("C:\\Users\\shay_deng\\Desktop\\test.xlsx")); workBook.write(outputStream); workBook.close(); // Remember to close the workbook } }
Read excel table information
public class ReadExcel07 { @Test public void readExcel() throws IOException{ FileInputStream inputStream = new FileInputStream( new File("C:\\Users\\shay_deng\\Desktop\\test.xlsx" )); // read workbook XSSFWorkbook workBook = new XSSFWorkbook(inputStream); // read work Sheet XSSFSheet sheet = workBook.getSheetAt(0 ); // read row XSSFRow row = sheet.getRow(0 ); // read cell XSSFCell cell = row.getCell(0 ); String value = cell.getStringCellValue(); System.out.println(value); inputStream.close(); // Close the workbook workBook.close(); } }
1. Merge cells, belong to worksheets, create independently, apply to worksheets
2. Styles, belonging to worksheets, created by workbooks, applied to cells
3. Fonts, belonging to worksheets, created by workbooks, applied to styles
4. To set the background color, be sure to set the color fill mode first
public class TestPOIExcelStyle { @Test public void testExcelStyle() throws IOException{ // 1. Create workbook HSSFWorkbook workBook = new HSSFWorkbook (); // Create merged cell object CellRangeAddress rangeAddress = new CellRangeAddress(2, 2, 2, 4 ); // Create style HSSFCellStyle style = workBook.createCellStyle(); style.setAlignment (HorizontalAlignment.CENTER); style.setVerticalAlignment(VerticalAlignment.CENTER); // Create font HSSFFont font = workBook.createFont(); font.setFontHeightInPoints(( short ) 16 ); // font.setFontHeight((short)320); The effect is the same as above. Use this method to set the size, the value should be set to font size * 20 times, see the API document font.setColor(HSSFColor.GREEN.index); font.setBold(true); style.setFont(font); // Set the background style.setFillPattern(FillPatternType.SOLID_FOREGROUND); style.setFillForegroundColor(HSSFColor.RED.index); // 2. Create worksheet HSSFSheet sheet = workBook.createSheet("helloWorld" ); // Add merge area sheet.addMergedRegion(rangeAddress); // 3. Create row HSSFRow row = sheet.createRow(2 ); // 4. Create cell HSSFCell cell = row.createCell(2 ); cell.setCellValue("helloWorld"); cell.setCellStyle(style); //输出 FileOutputStream outputStream = new FileOutputStream(new File("C:\\Users\\shay_deng\\Desktop\\test.xls")); workBook.write(outputStream); workBook.close(); outputStream.close(); } }