日志文章

2007-06-28

java 管理 Excel文件

package ExcelManage; 9~,eu  
htv~;!#~v  
import java.io.FileInputStream; Q:6.j2 NT  
import java.io.FileOutputStream; _'?GqMTN  
import java.text.*; 'UWVd4  
&fsxzF:}  
import org.apache.poi.hssf.usermodel.HSSFCell; X{4-$hh{~W  
import org.apache.poi.hssf.usermodel.HSSFRow; ,1 wTTh  
import org.apache.poi.hssf.usermodel.HSSFSheet; MhH%bZ5-  
import org.apache.poi.hssf.usermodel.HSSFWorkbook; nAPwve/;x  
import org.apache.poi.poifs.filesystem.POIFSFileSystem; g8\nsjy3c  
_ q6wd"  
public class ExcelOperation LKbCLr#3}  
{ Tl*$\t  
     private POIFSFileSystem poifsfilesystem; z[NHsdAM]  
     private HSSFWorkbook hssfworkbook; M~d^/Xw&  
     private HSSFSheet hssfsheet; Qu.dU%Q  
     private String excelfilename; Y"[{  
     /*Structure function get work sheet from given filename,default sheetindex is 0 */ [zRvR-W  
     public ExcelOperation(String filename) throws Exception{ Le?J&jTA  
           if ((filename==null)||(filename.equals(""))) "B`W LmQ  
           { # [!e`&  
                 throw new Exception("\n 读取的Excel文件名不能为空!"); .J&Z| }m6  
           } xrH&U>q?  
           if (!(filename.substring(filename.length() - 4).equals(".xls"))) ?E*f!:oY  
           { M,WO06Ff  
                 throw new Exception("\n 需要读取的Excel文件扩展名应为.xls"); Av2`y" 4vI  
           } \'ga!E  
           excelfilename = filename; VgTi& v:K  
           poifsfilesystem = new POIFSFileSystem(new FileInputStream(excelfilename)); k65<X ;  
           hssfworkbook = new HSSFWorkbook(poifsfilesystem); ;6~56wh{  
           hssfsheet = hssfworkbook.getSheetAt(Integer.parseInt("0")); BAh@juU/b  
     } k7HsVB#`5  
QJ0S?Z[  
     /*Structure function get work sheet from given filename and given sheetno uXG| 8d  
     sheetno begins from 0*/ g{&hy  
     public ExcelOperation(String filename,int sheetno) throws Exception{ J3Vb@  
           if ((filename==null)||(filename.equals(""))) Ba (V'e  
           { tRWvvo>Z  
                 throw new Exception("\n 读取的Excel文件名不能为空!"); "1kP@M&m  
           } X Y9j_pL  
           if (!(filename.substring(filename.length() - 4).equals(".xls"))) K((/qn|J(  
           { ,fJ+CW.L  
                 throw new Exception("\n 需要读取的Excel文件扩展名应为.xls"); KZz?yA7tC4  
           } h: 6]~^5  
           poifsfilesystem = new POIFSFileSystem(new FileInputStream(filename)); ;Wf7  
           hssfworkbook = new HSSFWorkbook(poifsfilesystem); xk4g;+  
           if ((sheetno<0)||(sheetno>=hssfworkbook.getNumberOfSheets())) dvH,Ef_k  
           { :2!z ttJ  
                 throw new Exception("\n 需要读取的工作表序号应该大于等于0小于工作簿中的工作表总数"); "<H?v)S  
           } "@"_X\5Xy  
           hssfsheet = hssfworkbook.getSheetAt(sheetno); OWA) Wm#  
     } NoxB%V;*  
      \2:"G^m$q  
     /*get cell value by given rowno and colno,rowno and colno begins from 0*/ j  3Pw&  
     public String getExcelCell(int rowno,int colno) throws Exception { }4M80*zN#  
           if ((rowno<0)||(colno<0)) L2d1gpNH  
           { edy\R<wh  
                 return ""; ..D[aY ?.  
           } vNjIdQ u+/  
           HSSFRow hssfrow = hssfsheet.getRow(rowno); `|q.8[orh  
           //System.out.println("\n rowno:"+rowno+"\t colno:"+colno+"\t row:"+hssfrow); {W[@#l+~ P  
           if (hssfrow == null) { 5\`VaVj  
                 return ""; [|* Cq J  
           } ]Q`;N'\  
           HSSFCell hssfcell = hssfrow.getCell((short)colno); 7I\V\H  
           if (hssfcell == null) { Zp[%cR+  
                 return ""; 4(,?#}f+  
           } 1S0n]\<  
           //System.out.println("\n hssfcell.celltype"+hssfcell.getCellType()); ]3t!/(  
           String strvalue = ""; F>u o't  
           switch (hssfcell.getCellType()) { 0+PbV s  
           case 0: nu%I;-0&-  
4.sq/tW|vv  
                 strvalue = Double.toString(hssfcell.getNumericCellValue()); OLOzR73=V  
                 DecimalFormat format = (DecimalFormat) NumberFormat.getPercentInstance(); M\$^bC  
                 format.applyPattern("#####0.00##"); h_uZGgnb  
                 strvalue = format.format(hssfcell.getNumericCellValue()); mSl!Oa$o?  
                 if (strvalue.substring(strvalue.length() - 3).equals(".00")) { l^7%<VlxT  
                       strvalue = strvalue.substring(0,strvalue.length() - 3); Ik(qV=&  
                 } $\Ypk+_C  
                 break; ;"56N  
           case 1: {knO~Er_qC  
                 strvalue = hssfcell.getStringCellValue(); ,4W%a8\O  
                 break; z &`m,x^&  
           default: &aX64m/  
                 strvalue = hssfcell.getStringCellValue(); PDk&(  
                 break; U'kxs}V  
           } B chJ?Gl  
           if (strvalue == null) { K~>Zd  
                 strvalue=""; K-@,[Ztqu  
           } 5ra!E ~  
           strvalue = strvalue.trim(); }*kHIR'(1  
           return strvalue; v/C}`G"L  
     } SC8~pgzA  
9(do@   
     /*set cell value by given rowno and colno,rowno and colno begins from 0*/ O>ou.  
     public boolean setExcelCell(int rowno,int colno,String setstr) throws Exception { KGK*  0J  
           if ((rowno<0)||(colno<0)) g[~K`J  
           { JOi&UT#  
                 return false;  *]9xmm  
           } \sGv6(be  
           if (setstr == null) 39"#SD\UZ  
           { ;rA_ a-%  
                 return false; {V5)a'h  
           } )> zZJ1]c  
           try { nqXl}>#6t  
                 HSSFRow hssfrow = hssfsheet.getRow(rowno); yIp!__('  
//                  System.out.println("\n row:"+rowno+"\t colno:"+colno+"\t setstr:"+setstr); e>/8DQ =  
//                  System.out.println("\n out:"+hssfrow); T lyuD/6  
                 if (hssfrow == null) { Zy[e RxE&  
                       hssfrow = hssfsheet.createRow((short)rowno); _r<]:CD  
                 } 75 ywjE7  
                 HSSFCell hssfcell = hssfrow.getCell((short)colno); Y(c'UCv`<  
                 if (hssfcell == null) { V\|Ieu  
                       hssfcell = hssfrow.createCell((short)colno); 'MG`  
                 } =mKJC5  
//                  System.out.println("\n setstr:"+setstr.length()+"\t setstr.getBytes()" #%S%JzOw'  
//                              +setstr.getBytes().length); d.I!aJ.Bq;  
                 if (setstr.length() != setstr.getBytes().length){ M7I fy  
                       hssfcell.setEncoding(HSSFCell.ENCODING_UTF_16); ;z\M5"v  
                 } j} DjSx  
                 hssfcell.setCellValue(setstr); 8xtviV 8  
           } catch (Exception e) { ~??3 $|%  
                 // TODO: handle exception |kW( QM  
                 e.printStackTrace(); a"4`}]3sb  
           } f}TWI  
           return true; YMl+,,  
     } ZT.|kST \u  
6l6%|Aai  
     public boolean ExcelWrite() throws Exception { 0)9pogru  
           if ((excelfilename==null)||(excelfilename.equals(""))) !/g!  
           { /z'&1L!L  
                 return false; w=KiE {m  
           } bqk Z`  
           FileOutputStream fileOut = new FileOutputStream(excelfilename); AwZqp+Vg  
           hssfworkbook.write(fileOut); cZxL Zbsq  
           fileOut.close(); wl:Ky`B  
           return true; tkHbqjO!*  
     } >$(YDiDt  
HkgF[Z&P  
     public boolean ExcelWrite(String exceloutfile) throws Exception { ?Ml?(An  
           if ((exceloutfile==null)||(exceloutfile.equals(""))) ]|eE:8Q   
           { :=Vk K  
                 return false; 1U_eFrEq  
           } k\<L#7-sqF  
           FileOutputStream fileOut = new FileOutputStream(exceloutfile); (>gV9ihZ&  
           hssfworkbook.write(fileOut); 0UQ|CiXtH  
           fileOut.close(); D^Ze {  
           return true; !fE/ZW+  
     } 61)F'k,  
l'jVa"x3'  
     public static void main(String[] args) |a , ~{  
     { ]n#Ze3"g  
           System.out.println("Hello World!"); EuV7b3Z  
     } =,LnN*ne `  
}


类别: 学习笔记 |  评论(1) |  浏览(9180) |  收藏
一共有 1 条评论
vvstone 2008-04-18 11:59 Says:
怎么这篇文章这么招广告啊! 真烦人啊 回复都不能加链接了你那无聊的广告发上来除了恶心人没别的用处
发表评论
看不清楚,换一张