//首先请确保poi版本是尽量新的 我只试过两个版本 2.5的不行 然后下了3.7的就okay了。
//poi下载地址:http://www.apache.org/dyn/closer.cgi/poi/release
package excel;
import include.Path;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.hssf.usermodel.*;
public class Excel
{
private String sourceFileName;
private String targetFileName;
private HSSFWorkbook book;
private HSSFSheet sheet;
private FileInputStream fileinputstream;
private FileOutputStream fileoutputstream;
//根据需要修改sheet
public void edit()
{
HSSFRow row = this.sheet.getRow(0);
HSSFCell cell = row.getCell(0);
cell.setCellValue("cell_edit");
//
//...更多修改...
//
}
//打开excel文件
public void openfile(String order_id)
{
this.sourceFileName = (Path.getRootPath() + "book_store//order_template.xls"); //Path.getRootPath() 在另外一篇日志中有~
this.targetFileName = (Path.getRootPath() + "book_store//订单_"+order_id+".xls");
try{
this.fileinputstream = new FileInputStream(this.sourceFileName);
POIFSFileSystem poifsfilesystem = new POIFSFileSystem(fileinputstream);
this.book = new HSSFWorkbook(poifsfilesystem);
this.sheet = this.book.getSheetAt(0);
}catch (Exception exception)
{
exception.printStackTrace();
}
}
//关闭excel文件
public void closeFile()
{
try{
this.fileoutputstream = new FileOutputStream(this.targetFileName);
this.book.write(this.fileoutputstream);
fileoutputstream.flush();
fileoutputstream.close();
this.fileinputstream.close();
} catch (Exception exception) {
exception.printStackTrace();
}
}
//
//代开订单模板文件修改 再另存为一个新的excel文件
//
public static void main(String[] args)
{
Excel excel = new Excel();
String order_id = "201101241001";
excel.openfile(order_id);
excel.edit();
excel.closeFile();
}
}
http://hi.baidu.com/online_ghost/item/67c630a843f32f786dd45502