分享

用java将数据写入excel(整理)

 怕瓦落地007 2014-10-21
1. 笔记:

OutputStream os ;
Label label;
File file = new File(srcFilePath);
File[] list=file.listFiles();
os=new FileOutputStream(dstDir+"pnrdata_a330.xls");
WritableWorkbook wwb = Workbook.createWorkbook(os);//创建xls文件
WritableSheet ws = wwb.createSheet("PNRDATA",0);//设置sheet名
//写抬头

label = new Label(0,0,"PNR_ID");

ws.addCell(label);
label = new Label(1,0,"File_Name");
ws.addCell(label);
label = new Label(2,0,"Start_Index");
ws.addCell(label);
label = new Label(3,0,"End_Index");
ws.addCell(label);

wwb.write();

2.首先需要一个JXL包,下载地址:http://download.csdn.net/source/292830
(1)生成EXCEL需要手动写查询语句把ORACLE数据库中的数据查询出来,再通过操作写到EXCEL文件里面。
(2)通过EXCEL把数据读取到ORACLE,同样需要去读取EXCEL工作薄里面的内容,再通过INSERT语句去插入数据库操作。

示例:
包括从Excel读取数据,生成新的Excel,以及修改Excel

Java代码
  1. package common.util;
  2. import jxl.*;
  3. import jxl.format.UnderlineStyle;
  4. import jxl.write.*;
  5. import jxl.write.Number;
  6. import jxl.write.Boolean;
  7. import java.io.*;
  8. public class ExcelHandle
  9. {
  10. public ExcelHandle()
  11. {
  12. }
  13. /**
  14. * 读取Excel
  15. *
  16. * @param filePath
  17. */
  18. public static void readExcel(String filePath)
  19. {
  20. try
  21. {
  22. InputStream is = new FileInputStream(filePath);
  23. Workbook rwb = Workbook.getWorkbook(is);
  24. //Sheet st = rwb.getSheet("0")这里有两种方法获取sheet表,1为名字,而为下标,从0开始
  25. Sheet st = rwb.getSheet("original");
  26. Cell c00 = st.getCell(0,0);
  27. //通用的获取cell值的方式,返回字符串
  28. String strc00 = c00.getContents();
  29. //获得cell具体类型值的方式
  30. if(c00.getType() == CellType.LABEL)
  31. {
  32. LabelCell labelc00 = (LabelCell)c00;
  33. strc00 = labelc00.getString();
  34. }
  35. //输出
  36. System.out.println(strc00);
  37. //关闭
  38. rwb.close();
  39. }
  40. catch(Exception e)
  41. {
  42. e.printStackTrace();
  43. }
  44. }
  45. /**
  46. * 输出Excel
  47. *
  48. * @param os
  49. */
  50. public static void writeExcel(OutputStream os)
  51. {
  52. try
  53. {
  54. /**
  55. * 只能通过API提供的工厂方法来创建Workbook,而不能使用WritableWorkbook的构造函数,
  56. * 因为类WritableWorkbook的构造函数为protected类型
  57. * method(1)直接从目标文件中读取WritableWorkbook wwb = Workbook.createWorkbook(new File(targetfile));
  58. * method(2)如下实例所示 将WritableWorkbook直接写入到输出流
  59. */
  60. WritableWorkbook wwb = Workbook.createWorkbook(os);
  61. //创建Excel工作表 指定名称和位置
  62. WritableSheet ws = wwb.createSheet("Test Sheet 1",0);
  63. //**************往工作表中添加数据*****************
  64. //1.添加Label对象
  65. Label label = new Label(0,0,"this is a label test");
  66. ws.addCell(label);
  67. //添加带有字型Formatting对象
  68. WritableFont wf = new WritableFont(WritableFont.TIMES,18,WritableFont.BOLD,true);
  69. WritableCellFormat wcf = new WritableCellFormat(wf);
  70. Label labelcf = new Label(1,0,"this is a label test",wcf);
  71. ws.addCell(labelcf);
  72. //添加带有字体颜色的Formatting对象
  73. WritableFont wfc = new WritableFont(WritableFont.ARIAL,10,WritableFont.NO_BOLD,false,
  74. UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.RED);
  75. WritableCellFormat wcfFC = new WritableCellFormat(wfc);
  76. Label labelCF = new Label(1,0,"This is a Label Cell",wcfFC);
  77. ws.addCell(labelCF);
  78. //2.添加Number对象
  79. Number labelN = new Number(0,1,3.1415926);
  80. ws.addCell(labelN);
  81. //添加带有formatting的Number对象
  82. NumberFormat nf = new NumberFormat("#.##");
  83. WritableCellFormat wcfN = new WritableCellFormat(nf);
  84. Number labelNF = new jxl.write.Number(1,1,3.1415926,wcfN);
  85. ws.addCell(labelNF);
  86. //3.添加Boolean对象
  87. Boolean labelB = new jxl.write.Boolean(0,2,false);
  88. ws.addCell(labelB);
  89. //4.添加DateTime对象
  90. jxl.write.DateTime labelDT = new jxl.write.DateTime(0,3,new java.util.Date());
  91. ws.addCell(labelDT);
  92. //添加带有formatting的DateFormat对象
  93. DateFormat df = new DateFormat("dd MM yyyy hh:mm:ss");
  94. WritableCellFormat wcfDF = new WritableCellFormat(df);
  95. DateTime labelDTF = new DateTime(1,3,new java.util.Date(),wcfDF);
  96. ws.addCell(labelDTF);
  97. //添加图片对象,jxl只支持png格式图片
  98. File image = new File("f:\\2.png");
  99. WritableImage wimage = new WritableImage(0,1,2,2,image);
  100. ws.addImage(wimage);
  101. //写入工作表
  102. wwb.write();
  103. wwb.close();
  104. }
  105. catch(Exception e)
  106. {
  107. e.printStackTrace();
  108. }
  109. }
  110. /**
  111. * 拷贝后,进行修改,其中file1为被copy对象,file2为修改后创建的对象
  112. * 尽单元格原有的格式化修饰是不能去掉的,我们还是可以将新的单元格修饰加上去,
  113. * 以使单元格的内容以不同的形式表现
  114. * @param file1
  115. * @param file2
  116. */
  117. public static void modifyExcel(File file1,File file2)
  118. {
  119. try
  120. {
  121. Workbook rwb = Workbook.getWorkbook(file1);
  122. WritableWorkbook wwb = Workbook.createWorkbook(file2,rwb);//copy
  123. WritableSheet ws = wwb.getSheet(0);
  124. WritableCell wc = ws.getWritableCell(0,0);
  125. //判断单元格的类型,做出相应的转换
  126. if(wc.getType == CellType.LABEL)
  127. {
  128. Label label = (Label)wc;
  129. label.setString("The value has been modified");
  130. }
  131. wwb.write();
  132. wwb.close();
  133. rwb.close();
  134. }
  135. catch(Exception e)
  136. {
  137. e.printStackTrace();
  138. }
  139. }
  140. //测试
  141. public static void main(String[] args)
  142. {
  143. try
  144. {
  145. //读Excel
  146. ExcelHandle.readExcel("f:/testRead.xls");
  147. //输出Excel
  148. File fileWrite = new File("f:/testWrite.xls");
  149. fileWrite.createNewFile();
  150. OutputStream os = new FileOutputStream(fileWrite);
  151. ExcelHandle.writeExcel(os);
  152. //修改Excel
  153. ExcelHandle.modifyExcel(new file(""),new File(""));
  154. }
  155. catch(Exception e)
  156. {
  157. e.printStackTrace();
  158. }
  159. }
  160. }

2.在jsp中做相关测试,创建一个writeExcel.jsp

Java代码
  1. <%
  2. response.reset();//清除Buffer
  3. response.setContentType("application/vnd.ms-excel");
  4. File fileWrite = new File("f:/testWrite.xls");
  5. fileWrite.createNewFile();
  6. new FileOutputStream(fileWrite);
  7. ExcelHandle.writeExcel(new FileOutputStream(fileWrite));
  8. %>
  9. 在IE中浏览writeExcel.jsp就可以动态生成Excel文档了,其中response.setContentType("application/vnd.ms-excel");语句必须要,才能确保不乱码,在jsp中输入<%@page contentType="application/vnd.ms-excel;charset=GBK"%>不行。

 


 

 
 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多