使用POI导入和导出 Excel文件
來源:http://www.blogjava.net/caihualin/archive/2008/05/12/164724.html
?
1、ExcelWriter.java
package com.eruite.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
?* @author caihua
?*/
public class ExcelReader {
?private HSSFWorkbook wb = null;// book [includes sheet]
?private HSSFSheet sheet = null;
?private HSSFRow row = null;
?private int sheetNum = 0; // 第sheetnum個工作表
?private int rowNum = 0;
?private FileInputStream fis = null;
?private File file = null;
?public ExcelReader() {
?}
?public ExcelReader(File file) {
??this.file = file;
?}
?public void setRowNum(int rowNum) {
??this.rowNum = rowNum;
?}
?public void setSheetNum(int sheetNum) {
??this.sheetNum = sheetNum;
?}
?public void setFile(File file) {
??this.file = file;
?}
?/**
? * 讀取excel文件獲得HSSFWorkbook對象
? */
?public void open() throws IOException {
??fis = new FileInputStream(file);
??wb = new HSSFWorkbook(new POIFSFileSystem(fis));
??fis.close();
?}
?/**
? * 返回sheet表數目
? *
? * @return int
? */
?public int getSheetCount() {
??int sheetCount = -1;
??sheetCount = wb.getNumberOfSheets();
??return sheetCount;
?}
?/**
? * sheetNum下的記錄行數
? *
? * @return int
? */
?public int getRowCount() {
??if (wb == null)
???System.out.println("=============>WorkBook為空");
??HSSFSheet sheet = wb.getSheetAt(this.sheetNum);
??int rowCount = -1;
??rowCount = sheet.getLastRowNum();
??return rowCount;
?}
?/**
? * 讀取指定sheetNum的rowCount
? *
? * @param sheetNum
? * @return int
? */
?public int getRowCount(int sheetNum) {
??HSSFSheet sheet = wb.getSheetAt(sheetNum);
??int rowCount = -1;
??rowCount = sheet.getLastRowNum();
??return rowCount;
?}
?/**
? * 得到指定行的內容
? *
? * @param lineNum
? * @return String[]
? */
?public String[] readExcelLine(int lineNum) {
??return readExcelLine(this.sheetNum, lineNum);
?}
?/**
? * 指定工作表和行數的內容
? *
? * @param sheetNum
? * @param lineNum
? * @return String[]
? */
?public String[] readExcelLine(int sheetNum, int lineNum) {
??if (sheetNum < 0 || lineNum < 0)
???return null;
??String[] strExcelLine = null;
??try {
???sheet = wb.getSheetAt(sheetNum);
???row = sheet.getRow(lineNum);
???int cellCount = row.getLastCellNum();
???strExcelLine = new String[cellCount + 1];
???for (int i = 0; i <= cellCount; i++) {
????strExcelLine[i] = readStringExcelCell(lineNum, i);
???}
??} catch (Exception e) {
???e.printStackTrace();
??}
??return strExcelLine;
?}
?/**
? * 讀取指定列的內容
? *
? * @param cellNum
? * @return String
? */
?public String readStringExcelCell(int cellNum) {
??return readStringExcelCell(this.rowNum, cellNum);
?}
?/**
? * 指定行和列編號的內容
? *
? * @param rowNum
? * @param cellNum
? * @return String
? */
?public String readStringExcelCell(int rowNum, int cellNum) {
??return readStringExcelCell(this.sheetNum, rowNum, cellNum);
?}
?/**
? * 指定工作表、行、列下的內容
? *
? * @param sheetNum
? * @param rowNum
? * @param cellNum
? * @return String
? */
?public String readStringExcelCell(int sheetNum, int rowNum, int cellNum) {
??if (sheetNum < 0 || rowNum < 0)
???return "";
??String strExcelCell = "";
??try {
???sheet = wb.getSheetAt(sheetNum);
???row = sheet.getRow(rowNum);
???if (row.getCell((short) cellNum) != null) { // add this condition
????// judge
????switch (row.getCell((short) cellNum).getCellType()) {
????case HSSFCell.CELL_TYPE_FORMULA:
?????strExcelCell = "FORMULA ";
?????break;
????case HSSFCell.CELL_TYPE_NUMERIC: {
?????strExcelCell = String.valueOf(row.getCell((short) cellNum)
???????.getNumericCellValue());
????}
?????break;
????case HSSFCell.CELL_TYPE_STRING:
?????strExcelCell = row.getCell((short) cellNum)
???????.getStringCellValue();
?????break;
????case HSSFCell.CELL_TYPE_BLANK:
?????strExcelCell = "";
?????break;
????default:
?????strExcelCell = "";
?????break;
????}
???}
??} catch (Exception e) {
???e.printStackTrace();
??}
??return strExcelCell;
?}
?public static void main(String args[]) {
??File file = new File("C://qt.xls");
??ExcelReader readExcel = new ExcelReader(file);
??try {
???readExcel.open();
??} catch (IOException e) {
???e.printStackTrace();
??}
??readExcel.setSheetNum(0); // 設置讀取索引為0的工作表
??// 總行數
??int count = readExcel.getRowCount();
??for (int i = 0; i <= count; i++) {
???String[] rows = readExcel.readExcelLine(i);
???for (int j = 0; j < rows.length; j++) {
????System.out.print(rows[j] + " ");
???}
???System.out.print("/n");
??}
?}
}
2、ExcelWriter.java
package com.eruite.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Calendar;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
?* 生成導出Excel文件對象
?*
?* @author caihua
?*
?*/
public class ExcelWriter {
?// 設置cell編碼解決中文高位字節截斷
?private static short XLS_ENCODING = HSSFCell.ENCODING_UTF_16;
?// 定制浮點數格式
?private static String NUMBER_FORMAT = "#,##0.00";
?// 定制日期格式
?private static String DATE_FORMAT = "m/d/yy"; // "m/d/yy h:mm"
?private OutputStream out = null;
?private HSSFWorkbook workbook = null;
?private HSSFSheet sheet = null;
?private HSSFRow row = null;
?public ExcelWriter() {
?}
?/**
? * 初始化Excel
? *
? */
?public ExcelWriter(OutputStream out) {
??this.out = out;
??this.workbook = new HSSFWorkbook();
??this.sheet = workbook.createSheet();
?}
?/**
? * 導出Excel文件
? *
? * @throws IOException
? */
?public void export() throws FileNotFoundException, IOException {
??try {
???workbook.write(out);
???out.flush();
???out.close();
??} catch (FileNotFoundException e) {
???throw new IOException(" 生成導出Excel文件出錯! ", e);
??} catch (IOException e) {
???throw new IOException(" 寫入Excel文件出錯! ", e);
??}
?}
?/**
? * 增加一行
? *
? * @param index
? *??????????? 行號
? */
?public void createRow(int index) {
??this.row = this.sheet.createRow(index);
?}
?/**
? * 獲取單元格的值
? *
? * @param index
? *??????????? 列號
? */
?public String getCell(int index) {
??HSSFCell cell = this.row.getCell((short) index);
??String strExcelCell = "";
??if (cell != null) { // add this condition
???// judge
???switch (cell.getCellType()) {
???case HSSFCell.CELL_TYPE_FORMULA:
????strExcelCell = "FORMULA ";
????break;
???case HSSFCell.CELL_TYPE_NUMERIC: {
????strExcelCell = String.valueOf(cell.getNumericCellValue());
???}
????break;
???case HSSFCell.CELL_TYPE_STRING:
????strExcelCell = cell.getStringCellValue();
????break;
???case HSSFCell.CELL_TYPE_BLANK:
????strExcelCell = "";
????break;
???default:
????strExcelCell = "";
????break;
???}
??}
??return strExcelCell;
?}
?/**
? * 設置單元格
? *
? * @param index
? *??????????? 列號
? * @param value
? *??????????? 單元格填充值
? */
?public void setCell(int index, int value) {
??HSSFCell cell = this.row.createCell((short) index);
??cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
??cell.setCellValue(value);
?}
?/**
? * 設置單元格
? *
? * @param index
? *??????????? 列號
? * @param value
? *??????????? 單元格填充值
? */
?public void setCell(int index, double value) {
??HSSFCell cell = this.row.createCell((short) index);
??cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
??cell.setCellValue(value);
??HSSFCellStyle cellStyle = workbook.createCellStyle(); // 建立新的cell樣式
??HSSFDataFormat format = workbook.createDataFormat();
??cellStyle.setDataFormat(format.getFormat(NUMBER_FORMAT)); // 設置cell樣式為定制的浮點數格式
??cell.setCellStyle(cellStyle); // 設置該cell浮點數的顯示格式
?}
?/**
? * 設置單元格
? *
? * @param index
? *??????????? 列號
? * @param value
? *??????????? 單元格填充值
? */
?public void setCell(int index, String value) {
??HSSFCell cell = this.row.createCell((short) index);
??cell.setCellType(HSSFCell.CELL_TYPE_STRING);
??cell.setEncoding(XLS_ENCODING);
??cell.setCellValue(value);
?}
?/**
? * 設置單元格
? *
? * @param index
? *??????????? 列號
? * @param value
? *??????????? 單元格填充值
? */
?public void setCell(int index, Calendar value) {
??HSSFCell cell = this.row.createCell((short) index);
??cell.setEncoding(XLS_ENCODING);
??cell.setCellValue(value.getTime());
??HSSFCellStyle cellStyle = workbook.createCellStyle(); // 建立新的cell樣式
??cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat(DATE_FORMAT)); // 設置cell樣式為定制的日期格式
??cell.setCellStyle(cellStyle); // 設置該cell日期的顯示格式
?}
?public static void main(String[] args) {
??System.out.println(" 開始導出Excel文件 ");
??File f = new File("C://qt.xls");
??ExcelWriter e = new ExcelWriter();
??try {
???e = new ExcelWriter(new FileOutputStream(f));
??} catch (FileNotFoundException e1) {
???e1.printStackTrace();
??}
??e.createRow(0);
??e.setCell(0, "試題編碼 ");
??e.setCell(1, "題型");
??e.setCell(2, "分值");
??e.setCell(3, "難度");
??e.setCell(4, "級別");
??e.setCell(5, "知識點");
??e.createRow(1);
??e.setCell(0, "t1");
??e.setCell(1, 1);
??e.setCell(2, 3.0);
??e.setCell(3, 1);
??e.setCell(4, "重要");
??e.setCell(5, "專業");
??try {
???e.export();
???System.out.println(" 導出Excel文件[成功] ");
??} catch (IOException ex) {
???System.out.println(" 導出Excel文件[失敗] ");
???ex.printStackTrace();
??}
?}
}
總結
以上是生活随笔為你收集整理的使用POI导入和导出 Excel文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 部落冲突怎么邀请好友进部落(《部落冲突》
- 下一篇: 架构宣言: MDA 实战