Apache的Poi讀寫Excel比較強大,對xlsx和xls都能夠支持,當然也支持更多的設置,下面是Poi的demo。
詳細查看poi的api:http://poi.apache.org/spreadsheet/quick-guide.html
一、使用的包
maven的相關依賴座標
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.10-FINAL</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.10-FINAL</version>
</dependency>
二、Poi的demo
package org.andy.excel;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Calendar;
import java.util.Date;
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.CreationHelper;
import org.apache.poi.ss.usermodel.DateUtil;
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.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;
public class PoiExcelTest {
public static void readExcel(String filePath) {
if (null != filePath && !"".equals(filePath.trim())) {
Workbook workbook = null;
InputStream inputStream = null;
try {
inputStream = new FileInputStream(filePath);
workbook = WorkbookFactory.create(inputStream); //使用WorkbookFactory創建,會安裝xls或xlsx
Sheet sheet = workbook.getSheetAt(0);
if (null != sheet) {
for (Row row : sheet) {
for (Cell cell : row) {
switch (cell.getCellType()) {// 判斷內容類型
case Cell.CELL_TYPE_STRING:// string類型
System.out.print(cell.getRichStringCellValue()
.getString());
break;
case Cell.CELL_TYPE_NUMERIC: //數字類型
if (DateUtil.isCellDateFormatted(cell)) {//判斷是否是日期
System.out.print(cell.getDateCellValue());
} else {
System.out
.print(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN: //布爾類型
System.out.print(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA: //公式
System.out.print(cell.getCellFormula());
break;
default:
System.out.print("");
}
System.out.print(" ");
}
System.out.println();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != inputStream) {
try {
inputStream.close();
inputStream = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
public void writeExcel(String filePath) {
if (null != filePath && !"".equals(filePath.trim())) {
Workbook workbook = null;
//根據不同的excel格式創建workbook
if (filePath.trim().toLowerCase().endsWith(".xls")) {
workbook = new HSSFWorkbook();
} else if (filePath.trim().toLowerCase().endsWith(".xlsx")) {
workbook = new XSSFWorkbook();
} else {
return;
}
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(filePath);
CreationHelper createHelper = workbook.getCreationHelper();
Sheet sheet = workbook.createSheet();
Row row = sheet.createRow(0);
row.createCell(0).setCellValue("is string");
//string類型
//設置cell的樣式
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setDataFormat(createHelper.createDataFormat()
.getFormat("yyyy-MM-dd HH:mm:ss"));
Cell cell = row.createCell(1);
cell.setCellStyle(cellStyle);
cell.setCellValue(new Date());
//Calendar
row.createCell(2).setCellValue(Calendar.getInstance());
//布爾類
row.createCell(3).setCellValue(true);
//數字類型
row.createCell(4).setCellValue(200);
workbook.write(outputStream);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != outputStream) {
try {
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
@Test
public void read() {
readExcel("c:\\andy.xlsx");
}
@Test
public void write() {
writeExcel("c:\\andy.xlsx");
}
}