Add Apache POI dependency:
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.14</version> </dependency>
Create a POJO class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | package albert.practice.poi; import java.io.Serializable; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.ToString; @Data @ToString @AllArgsConstructor @NoArgsConstructor public class Issue implements Serializable { /** * */ private static final long serialVersionUID = 1L; private Integer id; private String subject; private String status; private String priority; } |
Sample code is as bellows:
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | package albert.practice.poi; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFWorkbook; 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 lombok.extern.slf4j.Slf4j; //http://www.codejava.net/coding/how-to-write-excel-files-in-java-using-apache-poi //http://viralpatel.net/blogs/java-read-write-excel-file-apache-poi/ @Slf4j public class PoiExample { private static String excelFile = "D:" + File.separator + "issue.xls"; public static void main(String[] args) throws IOException { PoiExample test = new PoiExample(); test.writeExcel(); test.readExcel(excelFile); } public List<Issue> readExcel(String excelFile) throws IOException { List<Issue> issues = new ArrayList<Issue>(); InputStream inputStream = null; Workbook workbook = null; try { // 1. Create a Workbook. inputStream = new FileInputStream(new File(excelFile)); workbook = new HSSFWorkbook(inputStream); // 2. Get first sheet Sheet sheet = workbook.getSheetAt(0); // 3. Iterate through each rows from first sheet Iterator<Row> rowIterator = sheet.iterator(); int rowCount = 1; while (rowIterator.hasNext()) { // (1) ignore header row if (rowCount == 1) { rowIterator.next(); rowCount++; } // (2) start to read each row from second row else { Row row = rowIterator.next(); Integer id = Double.valueOf(row.getCell(0).getNumericCellValue()).intValue(); String subject = row.getCell(1).getStringCellValue(); String status = row.getCell(2).getStringCellValue(); String priority = row.getCell(3).getStringCellValue(); Issue issue = new Issue(); issue.setId(id); issue.setSubject(subject); issue.setStatus(status); issue.setPriority(priority); issues.add(issue); } } } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (inputStream != null) { inputStream.close(); } if (workbook != null) { workbook.close(); } } for (Issue issue : issues) { log.debug("issue = " + issue.toString()); } return issues; } public void writeExcel() throws IOException { // 0. prepare dummy data List<Issue> issues = createDummyIssues(); // 1. Create a Workbook. Workbook workbook = new HSSFWorkbook(); // 2. Create a Sheet. Sheet sheet = workbook.createSheet("issue list"); // 3. create cell style CellStyle style = createCellStyle(workbook); // 4. Repeat the following steps until all data is processed: // (1) Create a Row. // (2) Create Cells in a Row. Apply formatting using CellStyle. int rowCount = 0; Row headerRow = sheet.createRow(rowCount); writeHeader(headerRow, style); for (Issue issue : issues) { Row row = sheet.createRow(++rowCount); writeDataForEachRow(issue, row, style); } // 5. auto resize column width for (int i = 0; i < 4; i++) { sheet.autoSizeColumn(i); } // 6. Write to an OutputStream. // 7. Close the output stream. FileOutputStream outputStream = null; try { outputStream = new FileOutputStream(excelFile); workbook.write(outputStream); log.debug("write issue data to excel file successfully"); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (outputStream != null) { outputStream.close(); } if (workbook != null) { workbook.close(); } } } private void writeHeader(Row headerRow, CellStyle style) { Cell cell = headerRow.createCell(0); cell.setCellValue("編號"); cell.setCellStyle(style); cell = headerRow.createCell(1); cell.setCellValue("主旨"); cell.setCellStyle(style); cell = headerRow.createCell(2); cell.setCellValue("狀態"); cell.setCellStyle(style); cell = headerRow.createCell(3); cell.setCellValue("優先"); cell.setCellStyle(style); } private void writeDataForEachRow(Issue issue, Row row, CellStyle style) { Cell cell = row.createCell(0); cell.setCellValue(issue.getId()); cell.setCellStyle(style); cell = row.createCell(1); cell.setCellValue(issue.getSubject()); cell.setCellStyle(style); cell = row.createCell(2); cell.setCellValue(issue.getStatus()); cell.setCellStyle(style); cell = row.createCell(3); cell.setCellValue(issue.getPriority()); cell.setCellStyle(style); } private CellStyle createCellStyle(Workbook workbook) { CellStyle cellStyle = workbook.createCellStyle(); cellStyle.setBorderBottom((short) 1); cellStyle.setBorderTop((short) 1); cellStyle.setBorderLeft((short) 1); cellStyle.setBorderRight((short) 1); cellStyle.setWrapText(true); return cellStyle; } private List<Issue> createDummyIssues() { Issue issue1 = new Issue(1, "查不到資料", "新建立", "正常"); Issue issue2 = new Issue(2, "新增時發生錯誤", "新建立", "高"); Issue issue3 = new Issue(3, "刪除失敗", "處理中", "高"); return Arrays.asList(issue1, issue2, issue3); } } |
Excel file looks like: https://github.com/junyuo/AlbertGitProject/blob/master/src/albert/practice/poi/issue.xls
Reference
[1] http://www.codejava.net/coding/how-to-write-excel-files-in-java-using-apache-poi
[2] http://viralpatel.net/blogs/java-read-write-excel-file-apache-poi/