最近在做JAVA操作CSV文件相关功能,顺序把JAVA操作EXCEL的相关功能学习一下
http://www-128.ibm.com/developerworks/cn/java/l-javaExcel/index.html?n-j-01231
一文对使用JAVA EXCEL API对EXCEL进行操作做了说明
以下是API的主页:
http://jexcelapi.sourceforge.net/
以下是具体的下载路径:
http://sourceforge.net/project/showfiles.php?group_id=79926
今天没有时间对他进行学习了,改天再看。。。先记录到此
可以看下此文:
http://blog.youkuaiyun.com/tingqier/archive/2008/10/15/3079138.aspx
http://kingsui.iteye.com/blog/154712
已转贴到:
http://blog.youkuaiyun.com/luweifeng1983/archive/2008/12/30/3645807.aspx
http://www.iteye.com/topic/240053
下面是我的测试实例:
- packagecom.bytecode.openexcel.util;
- importjava.io.File;
- importjava.io.IOException;
- importjxl.Cell;
- importjxl.CellType;
- importjxl.Sheet;
- importjxl.Workbook;
- importjxl.read.biff.BiffException;
- importjxl.write.Label;
- importjxl.write.Number;
- importjxl.write.WritableCell;
- importjxl.write.WritableCellFormat;
- importjxl.write.WritableFont;
- importjxl.write.WritableSheet;
- importjxl.write.WritableWorkbook;
- importjxl.write.WriteException;
- importjxl.write.biff.RowsExceededException;
- publicclassMapExcel{
- publicstaticvoidmain(String[]args){
- Stringstep="";
- WorkbookworkBook=null;
- Cella=null;
- StringaStr="";
- try{
- step="readthexls";
- workBook=Workbook.getWorkbook(newFile("c:/myfile.xls"));
- Sheetsheet=workBook.getSheet(0);
- //按行读取sheet中的数据
- intskipLine=0;//可设置跳地读取的行数
- for(inti=skipLine;i<sheet.getRows();i++){
- Cell[]cells=sheet.getRow(i);
- for(intj=0;j<cells.length;j++){
- a=sheet.getCell(j,i);
- aStr=a.getContents();
- System.out.println(aStr);
- }
- }
- //按列读取sheet中的数据
- for(inti=0;i<sheet.getColumns();i++){
- Cell[]cells=sheet.getColumn(i);
- for(intj=0;j<cells.length;j++){
- a=sheet.getCell(i,j);
- aStr=a.getContents();
- System.out.println(aStr);
- }
- }
- }catch(BiffExceptione){
- e.printStackTrace();
- }catch(IOExceptione){
- e.printStackTrace();
- }finally{
- workBook.close();
- }
- step="writethexls";
- WritableWorkbookworkBook1=null;
- try{
- workBook1=Workbook.createWorkbook(newFile("c:/output.xls"));
- WritableSheetsheet1=workBook1.createSheet("Firstsheet",0);
- Labellabel=newLabel(0,2,"Alabelrecord");
- sheet1.addCell(label);
- Numbernumber=newNumber(3,4,3.1459);
- sheet1.addCell(number);
- step="addformatedcelltoxls";
- //CreateacellformatforArial10pointfont
- WritableFontarial10font=newWritableFont(WritableFont.ARIAL,10);
- WritableCellFormatarial10format=newWritableCellFormat(arial10font);
- //Createthelabel,specifyingcontentandformat
- //通过一种格式创建单元格
- Labellabel2=newLabel(1,0,"Arial10pointlabel",arial10format);
- sheet1.addCell(label2);
- //Cellformatsobjectsareshared,somanycellsmayusethesameformatobject,eg.
- //创建的单元格与前面创建的使用同样的格式
- Labellabel3=newLabel(2,0,"AnotherArial10pointlabel",arial10format);
- sheet1.addCell(label3);
- //可以使用其它的构造函数为某一个单元格单独列一种格式
- //CreateacellformatforTimes16,boldanditalic
- WritableFonttimes16font=newWritableFont(WritableFont.TIMES,16,WritableFont.BOLD,true);
- WritableCellFormattimes16format=newWritableCellFormat(times16font);
- //Createthelabel,specifyingcontentandformat
- Labellabel4=newLabel(3,0,"Times16bolditaliclabel",times16format);
- sheet1.addCell(label4);
- //相应的还有对数字和日期的格式,具体看JAVAEXCELAPI的指南文件
- }catch(RowsExceededExceptione1){
- e1.printStackTrace();
- }catch(WriteExceptione1){
- e1.printStackTrace();
- }catch(IOExceptione1){
- e1.printStackTrace();
- }finally{
- try{
- workBook1.write();
- workBook1.close();
- }catch(IOExceptione){
- e.printStackTrace();
- }catch(WriteExceptione){
- e.printStackTrace();
- }
- }
- step="copyandmodifyxls";
- WritableWorkbookcopy=null;
- try{
- WorkbookworkBook2=Workbook.getWorkbook(newFile("c:/myfile.xls"));
- //把myfile.xls中的内容(包括所有sheet)复制到out.xls中
- copy=Workbook.createWorkbook(newFile("c:/out.xls"),workBook2);
- WritableSheetsheet3=copy.getSheet(1);
- WritableCellcell3=sheet3.getWritableCell(1,2);
- if(cell3.getType()==CellType.LABEL){
- Labell=(Label)cell3;
- l.setString("modifycell");
- }
- }catch(BiffExceptione){
- e.printStackTrace();
- }catch(IOExceptione){
- e.printStackTrace();
- }finally{
- try{
- copy.write();
- copy.close();
- }catch(Exceptione){
- e.printStackTrace();
- }
- }
- }
- }