JAVA学习提高之----JAVA EXCEL API及JAVA 操作Excel(一)

本文介绍了一个Java程序如何读写Excel文件的具体实现。包括从Excel文件中读取数据,并向Excel文件中写入文本、数字及格式化内容的方法。此外,还展示了如何复制并修改Excel文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


最近在做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

下面是我的测试实例:

  1. packagecom.bytecode.openexcel.util;
  2. importjava.io.File;
  3. importjava.io.IOException;
  4. importjxl.Cell;
  5. importjxl.CellType;
  6. importjxl.Sheet;
  7. importjxl.Workbook;
  8. importjxl.read.biff.BiffException;
  9. importjxl.write.Label;
  10. importjxl.write.Number;
  11. importjxl.write.WritableCell;
  12. importjxl.write.WritableCellFormat;
  13. importjxl.write.WritableFont;
  14. importjxl.write.WritableSheet;
  15. importjxl.write.WritableWorkbook;
  16. importjxl.write.WriteException;
  17. importjxl.write.biff.RowsExceededException;
  18. publicclassMapExcel{
  19. publicstaticvoidmain(String[]args){
  20. Stringstep="";
  21. WorkbookworkBook=null;
  22. Cella=null;
  23. StringaStr="";
  24. try{
  25. step="readthexls";
  26. workBook=Workbook.getWorkbook(newFile("c:/myfile.xls"));
  27. Sheetsheet=workBook.getSheet(0);
  28. //按行读取sheet中的数据
  29. intskipLine=0;//可设置跳地读取的行数
  30. for(inti=skipLine;i<sheet.getRows();i++){
  31. Cell[]cells=sheet.getRow(i);
  32. for(intj=0;j<cells.length;j++){
  33. a=sheet.getCell(j,i);
  34. aStr=a.getContents();
  35. System.out.println(aStr);
  36. }
  37. }
  38. //按列读取sheet中的数据
  39. for(inti=0;i<sheet.getColumns();i++){
  40. Cell[]cells=sheet.getColumn(i);
  41. for(intj=0;j<cells.length;j++){
  42. a=sheet.getCell(i,j);
  43. aStr=a.getContents();
  44. System.out.println(aStr);
  45. }
  46. }
  47. }catch(BiffExceptione){
  48. e.printStackTrace();
  49. }catch(IOExceptione){
  50. e.printStackTrace();
  51. }finally{
  52. workBook.close();
  53. }
  54. step="writethexls";
  55. WritableWorkbookworkBook1=null;
  56. try{
  57. workBook1=Workbook.createWorkbook(newFile("c:/output.xls"));
  58. WritableSheetsheet1=workBook1.createSheet("Firstsheet",0);
  59. Labellabel=newLabel(0,2,"Alabelrecord");
  60. sheet1.addCell(label);
  61. Numbernumber=newNumber(3,4,3.1459);
  62. sheet1.addCell(number);
  63. step="addformatedcelltoxls";
  64. //CreateacellformatforArial10pointfont
  65. WritableFontarial10font=newWritableFont(WritableFont.ARIAL,10);
  66. WritableCellFormatarial10format=newWritableCellFormat(arial10font);
  67. //Createthelabel,specifyingcontentandformat
  68. //通过一种格式创建单元格
  69. Labellabel2=newLabel(1,0,"Arial10pointlabel",arial10format);
  70. sheet1.addCell(label2);
  71. //Cellformatsobjectsareshared,somanycellsmayusethesameformatobject,eg.
  72. //创建的单元格与前面创建的使用同样的格式
  73. Labellabel3=newLabel(2,0,"AnotherArial10pointlabel",arial10format);
  74. sheet1.addCell(label3);
  75. //可以使用其它的构造函数为某一个单元格单独列一种格式
  76. //CreateacellformatforTimes16,boldanditalic
  77. WritableFonttimes16font=newWritableFont(WritableFont.TIMES,16,WritableFont.BOLD,true);
  78. WritableCellFormattimes16format=newWritableCellFormat(times16font);
  79. //Createthelabel,specifyingcontentandformat
  80. Labellabel4=newLabel(3,0,"Times16bolditaliclabel",times16format);
  81. sheet1.addCell(label4);
  82. //相应的还有对数字和日期的格式,具体看JAVAEXCELAPI的指南文件
  83. }catch(RowsExceededExceptione1){
  84. e1.printStackTrace();
  85. }catch(WriteExceptione1){
  86. e1.printStackTrace();
  87. }catch(IOExceptione1){
  88. e1.printStackTrace();
  89. }finally{
  90. try{
  91. workBook1.write();
  92. workBook1.close();
  93. }catch(IOExceptione){
  94. e.printStackTrace();
  95. }catch(WriteExceptione){
  96. e.printStackTrace();
  97. }
  98. }
  99. step="copyandmodifyxls";
  100. WritableWorkbookcopy=null;
  101. try{
  102. WorkbookworkBook2=Workbook.getWorkbook(newFile("c:/myfile.xls"));
  103. //把myfile.xls中的内容(包括所有sheet)复制到out.xls中
  104. copy=Workbook.createWorkbook(newFile("c:/out.xls"),workBook2);
  105. WritableSheetsheet3=copy.getSheet(1);
  106. WritableCellcell3=sheet3.getWritableCell(1,2);
  107. if(cell3.getType()==CellType.LABEL){
  108. Labell=(Label)cell3;
  109. l.setString("modifycell");
  110. }
  111. }catch(BiffExceptione){
  112. e.printStackTrace();
  113. }catch(IOExceptione){
  114. e.printStackTrace();
  115. }finally{
  116. try{
  117. copy.write();
  118. copy.close();
  119. }catch(Exceptione){
  120. e.printStackTrace();
  121. }
  122. }
  123. }
  124. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值