谁能给我一个详细的Java通过Apache POI导出Excel方法,最好能给完整代码

2024-12-17 05:29:12
推荐回答(4个)
回答1:

package com.test;
/**
* 需要的jar包:
* poi-3.0.2-FINAL-20080204.jar
* poi-contrib-3.0.2-FINAL-20080204.jar
*poi-scratchpad-3.0.2-FINAL-20080204.jar
* poi-3.5-beta6-20090622.jar
* geronimo-stax-api_1.0_spec-1.0.jar
* ooxml-schemas-1.0.jar
* openxml4j-bin-beta.jar
* poi-ooxml-3.5-beta6-20090622.jar
*xmlbeans-2.3.0.jar
* dom4j-1.6.1.jar
*/
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;
import org.apache.poi.POIOLE2TextExtractor;
import org.apache.poi.POITextExtractor;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.extractor.ExtractorFactory;
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.hwpf.extractor.WordExtractor;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xslf.extractor.XSLFPowerPointExtractor;
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;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.xmlbeans.XmlException;
public class WordAndExcelExtractor {
public static void main(String[] args) {
try {
// 读取word
String wordFile = "D:/1.doc";
//String wordText2007 = WordAndExcelExtractor.extractTextFromDOC2007(wordFile);
//System.out.println("wordText2007=======" + wordText2007);
InputStream isword = new FileInputStream(wordFile);
WordExtractor wordExtractor = new WordExtractor(isword);
System.out.println("word========" + wordExtractor.getText());
// 读取 Excel
InputStream is = new FileInputStream("D:/测试.xls");
String excelText = WordAndExcelExtractor.extractTextFromXLS(is);
System.out.println("text2003==========" + excelText);
String excelFile = "D:/test2.xlsx";
String excelText2007 = WordAndExcelExtractor
.extractTextFromXLS2007(excelFile);
System.out.println("excelText2007==========" + excelText2007);
// 读取 PPT
PowerPointExtractor ppe = new PowerPointExtractor("D:/test.ppt");
System.out.println("ppt2003===============" + ppe.getText());
// System.out.println("###############################");
// System.out.println(ppe.getText(true, true, true, true));
//
// InputStream is = new FileInputStream("D:/test.ppt");
// PowerPointExtractor ppt2003 = new PowerPointExtractor(is);
// System.out.println(ppt2003.getText());
System.out.println("************************************");
XSLFPowerPointExtractor ppt = new XSLFPowerPointExtractor(
POIXMLDocument.openPackage("D:/test2.pptx"));
System.out.println("ppt2007============================="
+ ppt.getText());
/* 读取PDF */
InputStream in = new FileInputStream("D:/test.pdf");
PDDocument pdfDocument = PDDocument.load(in);
if (pdfDocument.isEncrypted()) {
// 仅仅尝试使用默认密码打开加密的PDF
pdfDocument.decrypt("");
}
PDFTextStripper stripper = null;
// 创建一个writer用来作来存储文件正文
StringWriter writer = new StringWriter();
if (stripper == null) {
stripper = new PDFTextStripper();
} else {
stripper.resetEngine();
}
stripper.writeText(pdfDocument, writer);
String contents = writer.getBuffer().toString();
System.out.println("pdfd===" + contents);

/* 向Word中写入数据 */
byte[] a = contents.getBytes();
ByteArrayInputStream bs = new ByteArrayInputStream(a);
POIFSFileSystem fs = new POIFSFileSystem();
// /////////////////////////////////
DirectoryEntry directory = fs.getRoot();
DocumentEntry de = directory.createDocument("WordDocument", bs);
// 以上两句代码不能省略,否则输出的是乱码
FileOutputStream fos = new FileOutputStream("D:\\dd.doc");
fs.writeFilesystem(fos);
bs.close();
fos.flush();
fos.close();
System.out.println("写入成功");

} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @Method: extractTextFromXLS
* @Description: 从excel 2003档中提取纯文本
*
* @param
* @return String
* @throws
*/
@SuppressWarnings("deprecation")
private static String extractTextFromXLS(InputStream is) throws IOException {
StringBuffer content = new StringBuffer();
HSSFWorkbook workbook = new HSSFWorkbook(is); // 创建对Excel工作簿文件的引用
for (int numSheets = 0; numSheets < workbook.getNumberOfSheets(); numSheets++) {
if (null != workbook.getSheetAt(numSheets)) {
HSSFSheet aSheet = workbook.getSheetAt(numSheets); // 获得一个sheet
content.append(aSheet.getSheetName());
content.append("\r\n-----------------------\r\n");
for (int rowNumOfSheet = 0; rowNumOfSheet <= aSheet
.getLastRowNum(); rowNumOfSheet++) {
if (null != aSheet.getRow(rowNumOfSheet)) {
HSSFRow aRow = aSheet.getRow(rowNumOfSheet); // 获得一行
for (short cellNumOfRow = 0; cellNumOfRow <= aRow
.getLastCellNum(); cellNumOfRow++) {
if (null != aRow.getCell(cellNumOfRow)) {
HSSFCell aCell = aRow.getCell(cellNumOfRow); // 获得列值
if (aCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
content.append(aCell.getNumericCellValue());
} else if (aCell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
content.append(aCell.getBooleanCellValue());
} else {
content.append(aCell.getStringCellValue());
}
content.append("\t");
}
}
content.append("\r\n");
}
}
}
}
return content.toString();
}
/**
* @Method: extractTextFromXLS2007
* @Description: 从excel 2007文档中提取纯文本
*
* @param
* @return String
* @throws
*/
private static String extractTextFromXLS2007(String fileName)
throws Exception {
StringBuffer content = new StringBuffer();
// 构造 XSSFWorkbook 对象,strPath 传入文件路径
XSSFWorkbook xwb = new XSSFWorkbook(fileName);
// 循环工作表Sheet
for (int numSheet = 0; numSheet < xwb.getNumberOfSheets(); numSheet++) {
XSSFSheet xSheet = xwb.getSheetAt(numSheet);
if (xSheet == null) {
continue;
}
// 循环行Row
for (int rowNum = 0; rowNum <= xSheet.getLastRowNum(); rowNum++) {
XSSFRow xRow = xSheet.getRow(rowNum);
if (xRow == null) {
continue;
}
// 循环列Cell
for (int cellNum = 0; cellNum <= xRow.getLastCellNum(); cellNum++) {
XSSFCell xCell = xRow.getCell(cellNum);
if (xCell == null) {
continue;
}
if (xCell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) {
content.append(xCell.getBooleanCellValue());
} else if (xCell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
content.append(xCell.getNumericCellValue());
} else {
content.append(xCell.getStringCellValue());
}
}
}
}
return content.toString();
}
}

这是POI jar包的下载地址,我下载的是3.9版本的
http://poi.apache.org/download.html

回答2:

/**
* 生成excel表格
* @param dataList 数据集合
* @param col 有多少列
* @param size 有几个对象
* @param col_name 每列的名称
* @return 返回excel表
* @throws Exception
*/
public HSSFWorkbook exportexcel(List dataList,int col,int size,String[] col_name) {
HSSFWorkbook workbook = null;
try {
// 这里的数据即是你要从后台取得的数据
// 创建工作簿实例
workbook = new HSSFWorkbook();
// 创建工作表实例
HSSFSheet sheet = workbook.createSheet("TscExcel");
// 设置列宽
this.setSheetColumnWidth(sheet,col);
// 获取样式
HSSFCellStyle style = this.createTitleStyle(workbook);
if (dataList != null && dataList.size() > 0) {
// 创建第一行标题,标题名字的本地信息通过resources从资源文件中获取
HSSFRow row = sheet.createRow((short) 0);// 建立新行
for(int c=0;c this.createCell(row, c, style, HSSFCell.CELL_TYPE_STRING, col_name[c]);//填充每列的对应名称
}
int j=0;
// 给excel填充数据
for (int i = 0; i < size; i++) {
HSSFRow row1 = sheet.createRow((short) (i + 1));// 建立新行
this.createCell(row1, 0, style, HSSFCell.CELL_TYPE_STRING, i+1);
for(int t=1;t this.createCell(row1, t, style, HSSFCell.CELL_TYPE_STRING, dataList.get(j++));
}
}
}else{
this.createCell(sheet.createRow(0), 0, style, HSSFCell.CELL_TYPE_STRING, "查无资料");
}
} catch (Exception e) {
e.printStackTrace();
}
return workbook;
}

private void setSheetColumnWidth(HSSFSheet sheet , int t) {
// 根据你数据里面的记录有多少列,就设置多少列
for(int i=0;i if(i==0){
sheet.setColumnWidth((short) i, (short) 2000);
}else{
sheet.setColumnWidth((short) i, (short) 5000);
}
}
}

回答3:

http://blog.csdn.net/yaohucaizi/article/details/8852872# 这上面有poi操作Excel的方法

回答4:

这是在开发中操作excel等等是最常见不过的问题了,今天给大家分享一下Apache POI导出Excel方法,ExportExcel 可以直接copy过去改改就可以用
代码如下:
01.package com.smnpc.util;

02.

03.import java.io.FileOutputStream;

04.import java.io.IOException;

05.import java.util.Calendar;

06.

07.import org.apache.poi.hssf.usermodel.HSSFCell;

08.import org.apache.poi.hssf.usermodel.HSSFCellStyle;

09.import org.apache.poi.hssf.usermodel.HSSFDataFormat;

10.import org.apache.poi.hssf.usermodel.HSSFRow;

11.import org.apache.poi.hssf.usermodel.HSSFSheet;

12.import org.apache.poi.hssf.usermodel.HSSFWorkbook;

13.

14./**

15.* 生成导出Excel文件对象

16.*
17.* @author Robert

18.*
19.*/

20.public class ExportExcel {

21.// 设置cell编码解决中文高位字节截断

22.// private static short XLS_ENCODING = HSSFWorkbook.ENCODING_UTF_16;

23.// 定制日期格式

24.private static String DATE_FORMAT = " m/d/yy "; // "m/d/yy h:mm"

25.// 定制浮点数格式

26.private static String NUMBER_FORMAT = " #,##0.00 ";

27.

28.private String xlsFileName;

29.

30.private HSSFWorkbook workbook;

31.

32.private HSSFSheet sheet;

33.

34.private HSSFRow row;

35.

36./**

37.* 初始化Excel

38.*
39.* @param fileName

40.* 导出文件名

41.* @return

42.*/

43.public void XLSExport(String fileName) {

44.this.xlsFileName = fileName;

45.this.workbook = new HSSFWorkbook();

46.this.sheet = workbook.createSheet();

47.}

48.

49./**

50.* 导出Excel文件

51.*
52.* @throws IOException

53.* @throws XLSException

54.*/

55.public void exportXLS() throws IOException {

56.FileOutputStream fOut = new FileOutputStream(xlsFileName);

57.workbook.write(fOut);

58.fOut.flush();

59.fOut.close();

60.}

61.

62./**

63.* 增加一行

64.*
65.* @param index

66.* 行号

67.*/

68.public void createRow(int index) {

69.this.row = this.sheet.createRow(index);

70.}

71.

72./**

73.* 设置单元格

74.*
75.* @param index

76.* 列号

77.* @param value

78.* 单元格填充值

79.*/

80.public void setCell(int index, String value) {

81.HSSFCell cell = this.row.createCell((short) index);

82.cell.setCellType(HSSFCell.CELL_TYPE_STRING);

83.// cell.setEncoding(XLS_ENCODING);

84.cell.setCellValue(value);

85.}

86.

87./**

88.* 设置单元格

89.*
90.* @param index

91.* 列号

92.* @param value

93.* 单元格填充值

94.*/

95.public void setCell(int index, Calendar value) {

96.HSSFCell cell = this.row.createCell((short) index);

97.// cell.setEncoding(XLS_ENCODING);

98.cell.setCellValue(value.getTime());

99.HSSFCellStyle cellStyle = workbook.createCellStyle(); // 建立新的cell样式

100.cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat(DATE_FORMAT)); // 设置cell样式为定制的日期格式

101.cell.setCellStyle(cellStyle); // 设置该cell日期的显示格式

102.}

103.

104./**

105.* 设置单元格

106.*
107.* @param index

108.* 列号

109.* @param value

110.* 单元格填充值

111.*/

112.public void setCell(int index, int value) {

113.HSSFCell cell = this.row.createCell((short) index);

114.cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);

115.cell.setCellValue(value);

116.}

117.

118./**

119.* 设置单元格

120.*
121.* @param index

122.* 列号

123.* @param value

124.* 单元格填充值

125.*/

126.public void setCell(int index, double value) {

127.HSSFCell cell = this.row.createCell((short) index);

128.cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);

129.cell.setCellValue(value);

130.HSSFCellStyle cellStyle = workbook.createCellStyle(); // 建立新的cell样式

131.HSSFDataFormat format = workbook.createDataFormat();

132.cellStyle.setDataFormat(format.getFormat(NUMBER_FORMAT)); // 设置cell样式为定制的浮点数格式

133.cell.setCellStyle(cellStyle); // 设置该cell浮点数的显示格式

134.}

135.

136.

137.public static void main(String[] args) {

138.ExportExcel excel = new ExportExcel();

139.excel.XLSExport("d:\\测试.xls");

140.excel.createRow(0);

141.excel.setCell(0, "序号");

142.excel.setCell(1, "公司");

143.excel.setCell(2, "网址");

144.excel.setCell(3, "姓名");

145.excel.createRow(1);// excel正文

146.excel.setCell(0, "1");

147.excel.setCell(1, "程序员之家");

148.excel.setCell(2, "http://bbs.it-home.org");

149.excel.setCell(3, "小贝");

150.try {

151.excel.exportXLS();

152.System.out.println("导出excel成功");

153.} catch (IOException e) {

154.System.out.println("导出excel失败");

155.e.printStackTrace();

156.}

157.}

158.}