ApachePOI如何实现Excel文件的读写操作?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1261个文字,预计阅读时间需要6分钟。
javapackage com.sefon.construct.utils.excel;
import java.util.List;
public class DataSet { private List sheetList;
public List getSheetList() { return sheetList; }
public void setSheetList(List sheetList) { this.sheetList=sheetList; }}
DataSet.javapackage com.sefon.contruct.utils.excel;
import java.util.List;
public class DataSet {
private List
package com.sefon.contruct.utils.excel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
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.ss.usermodel.Cell;
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.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelOperClass {
private static String EXCEL_2003 = ".xls";
private static String EXCEL_2007 = ".xlsx";
/**
* 通过POI方式读取Excel
*
* @param excelFile
*/
public static DataSet readExcelPOI(String filePath, Integer cons) throws Exception {
File excelFile = new File(filePath);
if (excelFile != null) {
String fileName = excelFile.getName();
fileName = fileName.toLowerCase();
if (fileName.toLowerCase().endsWith(EXCEL_2003)) {
DataSet dataSet = readExcelPOI2003(excelFile, cons);
return dataSet;
}
if (fileName.toLowerCase().endsWith(EXCEL_2007)) {
DataSet dataSet = readExcelPOI2007(excelFile, cons);
return dataSet;
}
}
return null;
}
/**
* 将数据写入到Excel文件中
*
* @param filePath
* @param cons
* @param dataSet
* @throws Exception
*/
public static void writeExcelPOI(String filePath, DataSet dataSet) throws Exception {
if (filePath != null) {
File excelFile = new File(filePath);
String fileName = excelFile.getName();
Workbook workbook = null;
if (fileName.toLowerCase().endsWith(EXCEL_2003)) {
workbook = new HSSFWorkbook();
}
if (fileName.toLowerCase().endsWith(EXCEL_2007)) {
workbook = new XSSFWorkbook();
}
// 支持多sheet数据写入
List
package com.sefon.contruct.utils.excel;
import java.util.List;
public class ExcelSheet {
private String sheetName;
private String[] headers;
private List
本文共计1261个文字,预计阅读时间需要6分钟。
javapackage com.sefon.construct.utils.excel;
import java.util.List;
public class DataSet { private List sheetList;
public List getSheetList() { return sheetList; }
public void setSheetList(List sheetList) { this.sheetList=sheetList; }}
DataSet.javapackage com.sefon.contruct.utils.excel;
import java.util.List;
public class DataSet {
private List
package com.sefon.contruct.utils.excel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
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.ss.usermodel.Cell;
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.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelOperClass {
private static String EXCEL_2003 = ".xls";
private static String EXCEL_2007 = ".xlsx";
/**
* 通过POI方式读取Excel
*
* @param excelFile
*/
public static DataSet readExcelPOI(String filePath, Integer cons) throws Exception {
File excelFile = new File(filePath);
if (excelFile != null) {
String fileName = excelFile.getName();
fileName = fileName.toLowerCase();
if (fileName.toLowerCase().endsWith(EXCEL_2003)) {
DataSet dataSet = readExcelPOI2003(excelFile, cons);
return dataSet;
}
if (fileName.toLowerCase().endsWith(EXCEL_2007)) {
DataSet dataSet = readExcelPOI2007(excelFile, cons);
return dataSet;
}
}
return null;
}
/**
* 将数据写入到Excel文件中
*
* @param filePath
* @param cons
* @param dataSet
* @throws Exception
*/
public static void writeExcelPOI(String filePath, DataSet dataSet) throws Exception {
if (filePath != null) {
File excelFile = new File(filePath);
String fileName = excelFile.getName();
Workbook workbook = null;
if (fileName.toLowerCase().endsWith(EXCEL_2003)) {
workbook = new HSSFWorkbook();
}
if (fileName.toLowerCase().endsWith(EXCEL_2007)) {
workbook = new XSSFWorkbook();
}
// 支持多sheet数据写入
List
package com.sefon.contruct.utils.excel;
import java.util.List;
public class ExcelSheet {
private String sheetName;
private String[] headers;
private List

