1.poi读取word文档小例子:
import java.io.*;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
public class ReadDocFile {
public static void main(String[] args) {
File file = null;
WordExtractor extractor = null ;
try {
file = new File("c:\\New.doc");
FileInputStream fis=new FileInputStream(file.getAbsolutePath());
HWPFDocument document=new HWPFDocument(fis);
extractor = new WordExtractor(document);
String [] fileData = extractor.getParagraphText();
for(int i=0;i<fileData.length;i++){
if(fileData[i] != null)
System.out.println(fileData[i]);
}
}
catch(Exception exep){}
}
}
2.读取Excel:
package com.cn.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class Test2 {
/**
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream(new File("D:/myExcel.xls"));//1、创建文件读取对象并指定要读取的文件路径
HSSFWorkbook workBook = new HSSFWorkbook(fis);//2、将要读取的文件对象放入工作簿对象中
int sheetNum = workBook.getNumberOfSheets();//3、获取当前工作簿的页数
for(int i=0;i<sheetNum;i++){//4、通过循环获取该工作簿的每一个sheet页
HSSFSheet childSheet = workBook.getSheetAt(i);//5、获取指定的sheet页
int rowNum = childSheet.getLastRowNum();//6、获取当前sheet页的行数
for(int j=0;j<rowNum;j++){//5、通过循环获取该sheet页的每一行
HSSFRow row = childSheet.getRow(j);//6、获取指定的行
int cellNum = row.getLastCellNum();//7、获取该行的单元格数目
for(int k=0;k<cellNum;k++){//8、通过for循环获得该行的每一个单元格
System.out.print(row.getCell(k).toString()+"\t");//9、打印输出每个单元格中的数据。
}
System.out.println();
}
}
}
}
3.从数据库读取数据写入Excel表格中:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
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;
public class PersonDao {
/*
* query database and packing the data into the Object(从数据库查询数据并封装到PersonModel对象中
*/
public static List<PersonModel> QueryPerson() throws SQLException{
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from persons";
conn = Utils.getConnection();
List<PersonModel> list = new ArrayList<PersonModel>();
try{
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
PersonModel model = new PersonModel();
model.setId(rs.getInt(1));
model.setName(rs.getString(2));
list.add(model);
}
}catch(SQLException e){
e.printStackTrace();
}finally{
Utils.closeDB(rs, ps, conn);
}
return list;
}
/*
* create Excel and write the data
*/
public void CreateExcel() throws SQLException, IOException{
HSSFWorkbook workBook = new HSSFWorkbook();//创建一个workBook工作簿对象
HSSFSheet sheet = workBook.createSheet("First sheet");//创建一个sheet页
sheet.setColumnWidth((short)0, 2500);
sheet.setColumnWidth((short)1, 5000);
HSSFRow row = sheet.createRow((short)0);
HSSFCell cell[] = new HSSFCell[2];
for(int i=0;i<2;i++){
cell[i] = row.createCell(i);
}
cell[0].setCellValue("id");
cell[1].setCellValue("name");
List<PersonModel> list = PersonDao.QueryPerson();
if(list != null && list.size() > 0){
for(int i=0;i<list.size();i++){
PersonModel model = list.get(i);
HSSFRow dataRow = sheet.createRow(i+1);
HSSFCell data[] = new HSSFCell[2];
for(int j=0;j<2;j++){
data[j] = dataRow.createCell(j);
}
data[0].setCellValue(model.getId());
data[1].setCellValue(model.getName());
File file = new File("D:\\person.xls");
FileOutputStream fos = new FileOutputStream(file);
workBook.write(fos);
fos.close();
}
}
}
public List<PersonModel> readExcel(){
List<PersonModel> list = new ArrayList<PersonModel>();
try{
FileInputStream fis = new FileInputStream("D:/myExcel.xls");
HSSFWorkbook workBook = new HSSFWorkbook(fis);
int sheetNum = workBook.getNumberOfSheets();//获取当前页数
for(int i=0;i<sheetNum;i++){
HSSFSheet childSheet = workBook.getSheetAt(i);
int rowNum = childSheet.getLastRowNum();//获取当前行数
for(int j=1;j<rowNum;j++){
HSSFRow row = childSheet.getRow(j);//获取当前行
//PersonModel model = new PersonModel();
//model.setId(Integer.parseInt(row.getCell(0).toString()));
//model.setName(row.getCell(1).toString());
//list.add(model);
Integer id =new Integer((int) row.getCell(0).getNumericCellValue());
//System.out.print("nihao");
//System.out.print(id);
insertDB(id,row.getCell(1).toString());
}
}
fis.close();
}catch(Exception e){
e.printStackTrace();
}
return list;
}
public static void insertDB(int id,String name) throws Exception{
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "insert into persons values(?,?)";
conn = Utils.getConnection();
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
ps.setString(2, name);
if(ps.executeUpdate() > 0){
System.out.println("Operaion succeed!");
}else{
System.out.println("Operation failed!");
}
Utils.closeDB(rs, ps, conn);
}
public static void main(String[] args) throws SQLException, IOException {
PersonDao dao = new PersonDao();
//dao.CreateExcel();
dao.readExcel();
//System.exit(0);
}
}
4.把word2003转换成html:
package poi;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.converter.PicturesManager;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.hwpf.usermodel.Picture;
import org.apache.poi.hwpf.usermodel.PictureType;
import org.w3c.dom.Document;
public class PoiWordToHtml {
public static void main(String[] args) throws Throwable {
final String path = "c:/temp/";
final String file = "1.doc";
InputStream input = new FileInputStream(path + file);
HWPFDocument wordDocument = new HWPFDocument(input);
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
wordToHtmlConverter.setPicturesManager(new PicturesManager() {
public String savePicture(byte[] content, PictureType pictureType, String suggestedName, float widthInches, float heightInches) {
return suggestedName;
}
});
wordToHtmlConverter.processDocument(wordDocument);
List pics = wordDocument.getPicturesTable().getAllPictures();
if (pics != null) {
for (int i = 0; i < pics.size(); i++) {
Picture pic = (Picture) pics.get(i);
try {
pic.writeImageContent(new FileOutputStream(path + pic.suggestFullFileName()));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Document htmlDocument = wordToHtmlConverter.getDocument();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
DOMSource domSource = new DOMSource(htmlDocument);
StreamResult streamResult = new StreamResult(outStream);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.METHOD, "html");
serializer.transform(domSource, streamResult);
outStream.close();
String content = new String(outStream.toByteArray());
FileUtils.write(new File(path, "1.html"), content, "utf-8");
}
}