由于項目需要從EXCEL文件中導入數據,所以這幾天上網收集了一下這方面的資料!
于是找到了POI這個玩意,本來想用JXL的,但了解到它對處理數據量大的時候,效率不行!.于是選擇了POI!
要求:JDK 1.4+POI開發包
可以到
http://www.apache.org/dyn/closer.cgi/jakarta/poi/ 下載
Jakarta POIJakarta POI可以讓你使用Java來讀寫MS Excel ,Word文件
相關文檔官方網站:
http://jakarta.apache.org/poi/ http://www.matrix.org.cn/down_view.asp?id=14 www.matrix.org.cn上的東西一向很不錯!!
創建Excel 文檔 示例1將演示如何利用Jakarta POI API 創建Excel 文檔。
示例1程序如下:
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import java.io.FileOutputStream;
public class CreateXL {
/** Excel 文件要存放的位置,假定在D盤下*/
public static String outputFile="D:\\test.xls";
public static void main(String argv[]){
try{
// 創建新的Excel 工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
// 在Excel工作簿中建一工作表,其名為缺省值
// 如要新建一名為"效益指標"的工作表,其語句為:
// HSSFSheet sheet = workbook.createSheet("效益指標");
HSSFSheet sheet = workbook.createSheet();
// 在索引0的位置創建行(最頂端的行)
HSSFRow row = sheet.createRow((short)0);
//在索引0的位置創建單元格(左上端)
HSSFCell cell = row.createCell((short) 0);
// 定義單元格為字符串類型
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
// 在單元格中輸入一些內容
cell.setCellValue("增加值");
// 新建一輸出文件流
FileOutputStream fOut = new FileOutputStream(outputFile);
// 把相應的Excel 工作簿存盤
workbook.write(fOut);
fOut.flush();
// 操作結束,關閉文件
fOut.close();
System.out.println("文件生成...");
}catch(Exception e) {
System.out.println("已運行 xlCreate() : " + e );
}
}
}
讀取Excel文檔中的數據 示例2將演示如何讀取Excel文檔中的數據。假定在D盤JTest目錄下有一個文件名為test1.xls的Excel文件。
示例2程序如下:
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import java.io.FileInputStream;
public class ReadXL {
/** Excel文件的存放位置。注意是正斜線*/
public static String fileToBeRead="D:\\test1.xls";
public static void main(String argv[]){
try{
// 創建對Excel工作簿文件的引用
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(fileToBeRead));
// 創建對工作表的引用。
// 本例是按名引用(讓我們假定那張表有著缺省名"Sheet1")
HSSFSheet sheet = workbook.getSheet("Sheet1");
// 也可用getSheetAt(int index)按索引引用,
// 在Excel文檔中,第一張工作表的缺省索引是0,
// 其語句為:HSSFSheet sheet = workbook.getSheetAt(0);
// 讀取左上端單元
HSSFRow row = sheet.getRow(0);
HSSFCell cell = row.getCell((short)0);
// 輸出單元內容,cell.getStringCellValue()就是取所在單元的值
System.out.println("左上端單元是: " + cell.getStringCellValue());
}catch(Exception e) {
System.out.println("已運行xlRead() : " + e );
}
}
}
設置單元格格式
在這里,我們將只介紹一些和格式設置有關的語句,我們假定workbook就是對一個工作簿的引用。在Java中,第一步要做的就是創建和設置字體和單元格的格式,然后再應用這些格式:
1、創建字體,設置其為紅色、粗體:
HSSFFont font = workbook.createFont();
font.setColor(HSSFFont.COLOR_RED);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
2、創建格式
HSSFCellStyle cellStyle= workbook.createCellStyle();
cellStyle.setFont(font);
3、應用格式
HSSFCell cell = row.createCell((short) 0);
cell.setCellStyle(cellStyle);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("標題 ");
處理WORD文檔import java.io.*;
import org.textmining.text.extraction.WordExtractor;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
public class TestPoi {
public TestPoi() {
}
public static void main(String args[]) throws Exception
{
FileInputStream in = new FileInputStream ("D:\\a.doc");
WordExtractor extractor = new WordExtractor();
String str = extractor.extractText(in);
//System.out.println("the result length is"+str.length());
System.out.println(str);
}
}
在審批流程中,加入處理前和處理后的數據處理,可以將審批流程外的業務處理或是流程額外程序出來分離出來。放在代碼前,代碼后處理。
public WfActivity assignComplete(WfTranstion wfTrans,String procId, String activityId,
String touserId,String memo,HttpServletRequest request)throws WfException {
WfActivity wfAct = null;
try {
CodeFormula.parseBeforeCode(wfTrans.getConnection(),procId,activityId,CodeFormula.apply_code,request);
CheckAgree.execute(wfTrans,procId, activityId, new WfUser(uname, pwd),流程自己處理的方法
touserId,memo);
CodeFormula.parseAfterCode(wfTrans.getConnection(),procId,activityId,CodeFormula.apply_code,request);
} catch (WfException e) {
wfAct = null;
throw e;
}
return wfAct;
}
package com.borland.samples.welcome;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.File;
public class ReadFile {
public ReadFile() {}
/**
* 刪除某個文件夾下的所有文件夾和文件
* @param delpath String
* @throws FileNotFoundException
* @throws IOException
* @return boolean
*/
public static boolean deletefile(String delpath) throws FileNotFoundException,
IOException {
try {
File file = new File(delpath);
if (!file.isDirectory()) {
System.out.println("1");
file.delete();
}
else if (file.isDirectory()) {
System.out.println("2");
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File delfile = new File(delpath + "\\" + filelist[i]);
if (!delfile.isDirectory()) {
System.out.println("path=" + delfile.getPath());
System.out.println("absolutepath=" + delfile.getAbsolutePath());
System.out.println("name=" + delfile.getName());
delfile.delete();
System.out.println("刪除文件成功");
}
else if (delfile.isDirectory()) {
deletefile(delpath + "\\" + filelist[i]);
}
}
file.delete();
}
}
catch (FileNotFoundException e) {
System.out.println("deletefile() Exception:" + e.getMessage());
}
return true;
}
/**
* 刪除某個文件夾下的所有文件夾和文件
* @param delpath String
* @throws FileNotFoundException
* @throws IOException
* @return boolean
*/
public static boolean readfile(String filepath) throws FileNotFoundException,
IOException {
try {
File file = new File(filepath);
if (!file.isDirectory()) {
System.out.println("文件");
System.out.println("path=" + file.getPath());
System.out.println("absolutepath=" + file.getAbsolutePath());
System.out.println("name=" + file.getName());
}
else if (file.isDirectory()) {
System.out.println("文件夾");
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(filepath + "\\" + filelist[i]);
if (!readfile.isDirectory()) {
System.out.println("path=" + readfile.getPath());
System.out.println("absolutepath=" + readfile.getAbsolutePath());
System.out.println("name=" + readfile.getName());
}
else if (readfile.isDirectory()) {
readfile(filepath + "\\" + filelist[i]);
}
}
}
}
catch (FileNotFoundException e) {
System.out.println("readfile() Exception:" + e.getMessage());
}
return true;
}
public static void main(String[] args) {
try {
readfile("D:/file");
//deletefile("D:/file");
}
catch (FileNotFoundException ex) {
}
catch (IOException ex) {
}
System.out.println("ok");
}
}