之前用过POI,Jacob,等其他几种方式读取office document,对比之下,还是POI比较靠谱,更新也即时~最近有项目要用,就小小的总结一下~
POI 3.8下载地址http://www.apache.org/dyn/closer.cgi/poi/dev/bin/poi-bin-3.8-beta5-20111217.zip
或者http://poi.apache.org/download.html
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel.examples;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
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.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
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.util.CellRangeAddress;
/**
* File for HSSF testing/examples
*
* THIS IS NOT THE MAIN HSSF FILE!! This is a utility for testing functionality.
* It does contain sample API usage that may be educational to regular API
* users.
*
* @see #main
* @author Andrew Oliver (acoliver at apache dot org)
*/
public final class HSSFReadWrite {
/**
* creates an {@link HSSFWorkbook} the specified OS filename.
*/
private static HSSFWorkbook readFile(String filename) throws IOException {
return new HSSFWorkbook(new FileInputStream(filename));
}
/**
* given a filename this outputs a sample sheet with just a set of
* rows/cells.
*/
private static void testCreateSampleSheet(String outputFilename) throws IOException {
int rownum;
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
HSSFCellStyle cs = wb.createCellStyle();
HSSFCellStyle cs2 = wb.createCellStyle();
HSSFCellStyle cs3 = wb.createCellStyle();
HSSFFont f = wb.createFont();
HSSFFont f2 = wb.createFont();
f.setFontHeightInPoints((short) 12);
f.setColor((short) 0xA);
f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
f2.setFontHeightInPoints((short) 10);
f2.setColor((short) 0xf);
f2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
cs.setFont(f);
cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("($#,##0_);[Red]($#,##0)"));
cs2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cs2.setFillPattern((short) 1); // fill w fg
cs2.setFillForegroundColor((short) 0xA);
cs2.setFont(f2);
wb.setSheetName(0, "HSSF Test");
for (rownum = 0; rownum < 300; rownum++) {
HSSFRow r = s.createRow(rownum);
if ((rownum % 2) == 0) {
r.setHeight((short) 0x249);
}
for (int cellnum = 0; cellnum < 50; cellnum += 2) {
HSSFCell c = r.createCell(cellnum);
c.setCellValue(rownum * 10000 + cellnum
+ (((double) rownum / 1000) + ((double) cellnum / 10000)));
if ((rownum % 2) == 0) {
c.setCellStyle(cs);
}
c = r.createCell(cellnum + 1);
c.setCellValue(new HSSFRichTextString("TEST"));
// 50 characters divided by 1/20th of a point
s.setColumnWidth(cellnum + 1, (int) (50 * 8 / 0.05));
if ((rownum % 2) == 0) {
c.setCellStyle(cs2);
}
}
}
// draw a thick black border on the row at the bottom using BLANKS
rownum++;
rownum++;
HSSFRow r = s.createRow(rownum);
cs3.setBorderBottom(HSSFCellStyle.BORDER_THICK);
for (int cellnum = 0; cellnum < 50; cellnum++) {
HSSFCell c = r.createCell(cellnum);
c.setCellStyle(cs3);
}
s.addMergedRegion(new CellRangeAddress(0, 3, 0, 3));
s.addMergedRegion(new CellRangeAddress(100, 110, 100, 110));
// end draw thick black border
// create a sheet, set its title then delete it
s = wb.createSheet();
wb.setSheetName(1, "DeletedSheet");
wb.removeSheetAt(1);
// end deleted sheet
FileOutputStream out = new FileOutputStream(outputFilename);
wb.write(out);
out.close();
}
/**
* Method main
*
* Given 1 argument takes that as the filename, inputs it and dumps the
* cell values/types out to sys.out.<br/>
*
* given 2 arguments where the second argument is the word "write" and the
* first is the filename - writes out a sample (test) spreadsheet
* see {@link HSSFReadWrite#testCreateSampleSheet(String)}.<br/>
*
* given 2 arguments where the first is an input filename and the second
* an output filename (not write), attempts to fully read in the
* spreadsheet and fully write it out.<br/>
*
* given 3 arguments where the first is an input filename and the second an
* output filename (not write) and the third is "modify1", attempts to read in the
* spreadsheet, deletes rows 0-24, 74-99. Changes cell at row 39, col 3 to
* "MODIFIED CELL" then writes it out. Hence this is "modify test 1". If you
* take the output from the write test, you'll have a valid scenario.
*/
public static void main(String[] args) {
if (args.length < 1) {
System.err.println("At least one argument expected");
return;
}
String fileName = args[0];
try {
if (args.length < 2) {
HSSFWorkbook wb = HSSFReadWrite.readFile(fileName);
System.out.println("Data dump:\n");
for (int k = 0; k < wb.getNumberOfSheets(); k++) {
HSSFSheet sheet = wb.getSheetAt(k);
int rows = sheet.getPhysicalNumberOfRows();
System.out.println("Sheet " + k + " \"" + wb.getSheetName(k) + "\" has " + rows
+ " row(s).");
for (int r = 0; r < rows; r++) {
HSSFRow row = sheet.getRow(r);
if (row == null) {
continue;
}
int cells = row.getPhysicalNumberOfCells();
System.out.println("\nROW " + row.getRowNum() + " has " + cells
+ " cell(s).");
for (int c = 0; c < cells; c++) {
HSSFCell cell = row.getCell(c);
String value = null;
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_FORMULA:
value = "FORMULA value=" + cell.getCellFormula();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
value = "NUMERIC value=" + cell.getNumericCellValue();
break;
case HSSFCell.CELL_TYPE_STRING:
value = "STRING value=" + cell.getStringCellValue();
break;
default:
}
System.out.println("CELL col=" + cell.getColumnIndex() + " VALUE="
+ value);
}
}
}
} else if (args.length == 2) {
if (args[1].toLowerCase().equals("write")) {
System.out.println("Write mode");
long time = System.currentTimeMillis();
HSSFReadWrite.testCreateSampleSheet(fileName);
System.out.println("" + (System.currentTimeMillis() - time)
+ " ms generation time");
} else {
System.out.println("readwrite test");
HSSFWorkbook wb = HSSFReadWrite.readFile(fileName);
FileOutputStream stream = new FileOutputStream(args[1]);
wb.write(stream);
stream.close();
}
} else if (args.length == 3 && args[2].toLowerCase().equals("modify1")) {
// delete row 0-24, row 74 - 99 && change cell 3 on row 39 to string "MODIFIED CELL!!"
HSSFWorkbook wb = HSSFReadWrite.readFile(fileName);
FileOutputStream stream = new FileOutputStream(args[1]);
HSSFSheet sheet = wb.getSheetAt(0);
for (int k = 0; k < 25; k++) {
HSSFRow row = sheet.getRow(k);
sheet.removeRow(row);
}
for (int k = 74; k < 100; k++) {
HSSFRow row = sheet.getRow(k);
sheet.removeRow(row);
}
HSSFRow row = sheet.getRow(39);
HSSFCell cell = row.getCell(3);
cell.setCellValue("MODIFIED CELL!!!!!");
wb.write(stream);
stream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
分享到:
相关推荐
Java 操作Excel poi读写excel,所需要的jar包 https://blog.csdn.net/u014646662/article/details/83217382
poi读写excel,poi总结; poi读写excel,poi总结 poi读写excel,poi总结
poi读写excel表的实例,用java实现,在eclipse里直接运行
java中poi读写excel封装工具类(兼容office2003和2007等版本),绝对可用的例子!
java 使用POI框架读写excel doc
POI读写excel(.xls/.xlsx)的Demo,直接import到myeclipse即可运行。 IDE:myeclipse 9.1 含jar包
apache POI,文件读写 ,excel 对于使用apache poi 解析微软excel的一些文件
ExcelDemo_Excel导出_下载_POI读excel_JXL读写excel_java读写excel_列宽_读取合并或拆分单元格内容
poi3.7.jar 博文链接:https://tws502934462.iteye.com/blog/1487679
java读写Excel,POI.JAR,Word内容读取
附件是java poi 3.17的lib库及其依赖库,全部下载引用后,可以进行excel的读写操作,无需引用其他lib库。poi-3.17适用于jdk1.7。 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft ...
Java实现Excel读写的poi 5.2.1版本jar
jar包在项目根目录下:javaexcel\src目录中 源程序在项目根目录下:src\com目录中其中: read.java是读excel,write.java是写excel
NULL 博文链接:https://mazilaidaige.iteye.com/blog/1725205
poi3.9读写EXCEL 支持2007
POI工具包可用来读写Excel,在java开发中可用来创建、导出信息到excel表格中,还可读取excel中的信息使用到应用程序中。
一个java用POI读写Excel的类封装了一个读取Excel数据的方法和一个写入Excel数据的方法,写入的话可从数据库读取或自己写一个List...
Java POI下载Excel模板 Excel带有下拉框的模板,并有详细注释,生成exlcel的下拉框,必填项标红,数字栏位防止科学计数法,并设置Excel标题样式
使用poi读写微软的excel文件和WPS的et文件,poi3.1读写报表文件,此包是代码需要下载poi3.1jar