import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.HashMap;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
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.DataFormat;
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.XSSFWorkbook;
public class ExcelReportFile {
private static final long serialVersionUID = 612381396391426330L;
private Workbook workBook = null;
private HashMap<Integer, CellStyle> numberCellStyles = null;
private CellStyle textCellStyle = null;
private static int DEFAULT_SHEET_INDEX = 0;
public ExcelReportFile(File file) throws FileNotFoundException, IOException {
try {
//读取office 2007版本以上
workBook = new XSSFWorkbook(new FileInputStream(file));
} catch (Exception ex) {
//读取office 2003版本以下
workBook = new HSSFWorkbook(new FileInputStream(file));
}
}
public Object getValue(int rowIndex, int colIndex) {
return readCellValue(rowIndex, colIndex);
}
/**
* 通过行号,列号读取excel单元格数据
*
* @param rowNo
* @param colNo
* @return
*/
public String getValueByNo(int rowNo, int colNo) {
Object rtnValue = readCellValue(rowNo - 1, colNo - 1);
String sValue = String.valueOf((rtnValue == null) ? "" : rtnValue);
return sValue;
}
private Object readCellValue(int rowIndex, int colIndex) {
Object sCellValue = null;
Row row = workBook.getSheetAt(0).getRow(rowIndex);
if (row != null) {
Cell cell = row.getCell(colIndex);
if (cell != null) {
int cellType = cell.getCellType();
// HSSFCell.CELL_TYPE_FORMULA
// Empty
if (cellType == Cell.CELL_TYPE_BLANK) {
sCellValue = null;
// int dCellValue = 0;
// sCellValue = dCellValue;
}
// String
if (cellType == Cell.CELL_TYPE_STRING) {
sCellValue = cell.getRichStringCellValue().getString()
.trim();
}
// Number
if (cellType == Cell.CELL_TYPE_NUMERIC) {
int dCellValue = (int) cell.getNumericCellValue();
sCellValue = dCellValue;
}
// formula
if (cellType == Cell.CELL_TYPE_FORMULA) {
sCellValue = cell.getCellFormula();
}
}
}
return sCellValue;
}
public void writeNumber(int sheetIndex, int rowIndex, int colIndex,
Number value, int scale) {
Cell cell = getCell(sheetIndex, rowIndex, colIndex);
// HSSFCellStyle cellStyle = getNumberCellStyle(scale);
// cell.setCellStyle(cellStyle);
cell.setCellValue(value.doubleValue());
}
public void writeText(int sheetIndex, int rowIndex, int colIndex,
String value) {
Cell cell = getCell(sheetIndex, rowIndex, colIndex);
// HSSFCellStyle cellStyle = getTextCellStyle();
// cell.setCellType(HSSFCell.CELL_TYPE_STRING);
// cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("text"));
// cell.setCellStyle(cellStyle);
cell.setCellValue(new HSSFRichTextString(value));
}
public void writeText(int rowIndex, int colIndex, String value) {
writeText(DEFAULT_SHEET_INDEX, rowIndex, colIndex, value);
}
public void writeTextByNo(int rowIndex, int colIndex, String value) {
writeText(DEFAULT_SHEET_INDEX, rowIndex - 1, colIndex - 1, value);
}
public void writeNumber(int rowIndex, int colIndex, Number value, int scale) {
writeNumber(DEFAULT_SHEET_INDEX, rowIndex, colIndex, value, scale);
}
public void writeNumberByNo(int rowIndex, int colIndex, Number value,
int scale) {
writeNumber(DEFAULT_SHEET_INDEX, rowIndex - 1, colIndex - 1, value,
scale);
}
private Cell getCell(int sheetIndex, int rowIndex, int colIndex) {
// Sheet
Sheet sheet = null;
try {
sheet = workBook.getSheetAt(sheetIndex);
} catch (IllegalArgumentException ex) {
sheet = workBook.createSheet();
}
// Row
Row row = null;
row = sheet.getRow(rowIndex);
if (row == null) {
row = sheet.createRow(rowIndex);
}
// Cell
Cell cell = null;
cell = row.getCell(colIndex);
if (cell == null) {
cell = row.createCell(colIndex);
}
return cell;
}
private CellStyle getNumberCellStyle(int scale) {
if (this.numberCellStyles == null) {
this.numberCellStyles = new HashMap<Integer, CellStyle>();
}
if (this.numberCellStyles.get(Integer.valueOf(scale)) == null) {
CellStyle numberCellStyle = workBook.createCellStyle();
StringBuilder zeroBd = new StringBuilder();
DataFormat format = this.workBook.createDataFormat();
zeroBd.append("0");
if (scale > 0) {
zeroBd.append(".");
for (int zCount = 0; zCount < scale; zCount++) {
zeroBd.append("0");
}
}
short doubleFormat = format.getFormat(zeroBd.toString());
numberCellStyle.setDataFormat(doubleFormat);
this.numberCellStyles.put(Integer.valueOf(scale), numberCellStyle);
return numberCellStyle;
} else {
return this.numberCellStyles.get(Integer.valueOf(scale));
}
}
public void write(OutputStream stream) throws IOException {
if (this.workBook != null && stream != null) {
this.workBook.write(stream);
}
}
private CellStyle getTextCellStyle() {
if (textCellStyle != null) {
return textCellStyle;
} else {
return this.workBook.createCellStyle();
}
}
/**
* 通过英文字母获得列号
*
* @param colIndexStr
* @return
*/
public static int convertColIndexString2Number(String colIndexStr) {
int len = colIndexStr.toUpperCase().length();
char[] chars = colIndexStr.toCharArray();
int col = 0;
for (int index = 0; index < len; index++) {
char ca = chars[index];
int charAInt = Character.getNumericValue('A');
int charInt = Character.getNumericValue(ca);
BigDecimal bg = new BigDecimal(26);
col = col + bg.pow(len - index - 1).intValue()
* (charInt - charAInt + 1);
}
return col;
}
}
所需jar包:
poi-3.8-20120326.jar
poi-ooxml-3.8-20120326.jar
poi-ooxml-schemas-3.8-20120326.jar
xbean.jar
缺一不可哦。
分享到:
相关推荐
poi读写office文件的源代码程序,包括需要的jar,读取excel、word、ppt文件的样例程序
Excel读写类库,封装其他几个有POI类库实现对Excel文件的读取和保存数据到Excel,比较实用强大! 此Excel读写类库,比调用DotNet自带office库,强大。自己已经比较,稳定性、效率都大大提高。 DotNet自带Excel类库...
POI是一套用Java写成的库,能够帮助开发者在没有安装微软Office的情况下读写Office 97-2003的文件,支持的文件格式包括xls, doc, ppt等。目前POI的稳定版本中仅支持Excel文件格式xls,其他的都属于不稳定版本(放在...
这是一套开源的库文件,无需安装Office软件即可读取或保存Office97-2003,Office2007格式的文件,生成Excel报表文件速度快。 NPOI,顾名思义,就是POI的.NET版本。那POI又是什么呢?POI是一套用Java写成的库,能够...
java读写excel源码pyexcel - 让你专注于数据,而不是文件格式 支持项目 如果贵公司已将 pyexcel 及其组件嵌入到创收产品中,请在 github 上支持我,或维护该项目并进一步开发。 如果您是个人,也欢迎您支持我,无论...
Apache POI是一个流行的Java库,用于读取和写入Microsoft Office格式的文件,特别是Excel。它是Apache软件基金会的一个开源项目,提供了多种API来处理Office文档,包括Word、Excel、PowerPoint等。Apache POI的主要...
Apache POI项目的任务是创建和维护Java API,以便根据Office Open XML标准(OOXML)和Microsoft的OLE 2复合文档格式(OLE2)操作各种文件格式。简而言之,您可以使用Java读写MS Excel文件。此外,您还可以使用Java...
POI是一套用Java写成的库,能够帮助开发者在没有安装微软Office的情况下读写Office 97-2003的文件,支持的文件格式包括xls, doc, ppt等。 NPOI有几大优势 第一,完全基于.NET 2.0,而非.NET 3.0/3.5。 第二,读写...
文档基于POI3.15版本。 Apache POI 是创建和维护操作各种符合Office Open XML(OOXML)标准和微软的OLE 2复合文档格式(OLE2)的Java API。用它可以使用Java读取和创建,修改MS Excel文件.而且,还可以使用Java读取和...
POI提供API给Java程式对Microsoft Office格式档案读和写的功能。 结构: HSSF - 提供读写Microsoft Excel格式档案的功能。 XSSF - 提供读写Microsoft Excel OOXML格式档案的功能。 HWPF - 提供读写...
POI库是Apache提供的用于在Windows下读写各类微软Office文档的Java库,这里我们就来看一下Java使用Apache POI库读取Excel表格文档的示例:
使用NPOI,您可以非常轻松地读取/写入Office 2003/2007文件。 NPOI入门 贡献者 非常感谢所有!!! 捐款 如果这个项目可以帮助您减少开发时间,可以给我一杯啤酒 :beer_mug: 电报用户组 加入我们的电报: : ...
Asp.net操作Excel已经是老生长谈的事情了,可下面我说的这个NPOI操作Excel,应该是最好的方案了,没有之一,使用NPOI能够帮助开发者在没有安装微软Office的情况下读写Office 97-2003的文件,支持的文件格式包括xls, ...
NPOI 2.5.1 dll包,用于C#操作excel读取...NPOI是指构建在POI 3.x版本之上的一个程序,NPOI可以在没有安装Office的情况下对Word或Excel文档进行读写操作。NPOI是一个开源的C#读写Excel、WORD等微软OLE2组件文档的项目。
Format的各种格式文件,可以通过这些API在Java中读写Excel、Word等文件。他的excel处理很强大,对于word还局限于读取,目前只能实现一些简单文件的操作,不能设置样式。 3:Java2word是一个在java程序中调用 MS ...