import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Iterator; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelFileTest { public static void main(String[] args) { System.out.println("-------xlsx------"); writeExcelFile("D:\\ReadFileTest\\ExcelFileTest.xlsx"); readExcelFile("D:\\ReadFileTest\\ExcelFileTest.xlsx"); System.out.println("-------xls------"); writeExcelFile("D:\\ReadFileTest\\ExcelFileTest.xls"); readExcelFile("D:\\ReadFileTest\\ExcelFileTest.xls"); } // data input public static void writeExcelFile(String fileName) { String headString[] = { "User ID", "User Nickname", "User Phone Number", "User Nickname", "User Phone Number" }; boolean isE2007 = false; // Determine whether it is in excel2007 format if (fileName.endsWith("xlsx")) { isE2007 = true; } Workbook workbook = null;//Initialize according to the file format (2003 or 2007) if (isE2007) { workbook = new XSSFWorkbook(); } else { workbook = new HSSFWorkbook(); } Sheet sheet = workbook.createSheet("123"); try { Row row = null; Cell cell = null; int Rows = 0; row = sheet.createRow(Rows); for (int i = 0; i < headString.length; i++) { cell = row.createCell(i); System.out.println("headString[" + i + "] == " + headString[i]); cell.setCellValue(headString[i]); cell.setCellType(XSSFCell.CELL_TYPE_STRING); CellStyle cellStyle = workbook.createCellStyle(); cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);// Add this to make the color work cellStyle.setFillBackgroundColor(HSSFColor.GREEN.index);// Set the background color of the cell cellStyle.setFillForegroundColor(HSSFColor.BLUE.index);// Set the cell display color cell.setCellStyle(cellStyle); } FileOutputStream fileOut = new FileOutputStream(fileName); workbook.write(fileOut); fileOut.close(); } catch (FileNotFoundException e) { System.out.println("Cannot find the specified file"); } catch (IOException e) { System.out.println("Failed to read file"); } } // read data public static void readExcelFile(String fileName) { boolean isE2007 = false; // Determine whether it is in excel2007 format if (fileName.endsWith("xlsx")) { isE2007 = true; } try { System.out.println("------------InputStream"); InputStream inputStream = new FileInputStream(fileName); // Create an input stream Workbook workbook = null; // Initialize according to the file format (2003 or 2007) if (isE2007) { workbook = new XSSFWorkbook(inputStream); } else { workbook = new HSSFWorkbook(inputStream); } System.out.println("------------HSSFWorkbook"); Sheet sheet = workbook.getSheetAt(0); // get the first sheet // Read the data of the specified unit Row row2 = sheet.getRow(0); Cell cell2 = row2.getCell(0); System.out.println("cellColumnIndex == " + cell2.getColumnIndex()); System.out.println("cellColumnIndex == " + cell2.getCellType()); System.out.println("cellColumnIndex == " + cell2.getStringCellValue()); // iterator reads all data Iterator<Row> rows = sheet.rowIterator(); // get the iterator for the first sheet while (rows.hasNext()) { Row row = rows.next(); // get row data System.out.println("Row #" + row.getRowNum()); // get row number starting from 0 Iterator<Cell> cells = row.cellIterator(); // get the iterator for the first row while (cells.hasNext()) { Cell cell = cells.next(); System.out.println("Cell #" + cell.getColumnIndex()); switch (cell.getCellType()) { // output data according to the type in the cell case HSSFCell.CELL_TYPE_NUMERIC: System.out.println(cell.getNumericCellValue()); break; case HSSFCell.CELL_TYPE_STRING: System.out.println(cell.getStringCellValue()); break; case HSSFCell.CELL_TYPE_BOOLEAN: System.out.println(cell.getBooleanCellValue()); break; case HSSFCell.CELL_TYPE_FORMULA: System.out.println(cell.getCellFormula()); break; default: System.out.println("unsuported sell type"); break; } } } } catch (IOException ex) { ex.printStackTrace(); } } }
The relevant jar packages are as follows: