1. 1 package com.tgb.test;
  2. 2
  3. 3 import java.io.File;
  4. 4 import java.io.IOException;
  5. 5 import java.util.ArrayList;
  6. 6 import java.util.List;
  7. 7
  8. 8 import jxl.Cell;
  9. 9 import jxl.Sheet;
  10. 10 import jxl.Workbook;
  11. 11 import jxl.write.Label;
  12. 12 import jxl.write.Number;
  13. 13 import jxl.write.WritableImage;
  14. 14 import jxl.write.WritableSheet;
  15. 15 import jxl.write.WritableWorkbook;
  16. 16 import jxl.write.WriteException;
  17. 17
  18. 18 import org.apache.commons.lang3.math.NumberUtils;
  19. 19 import org.junit.Test;
  20. 20 //import org.junit.Test;
  21. 21 public class JxlDemo {
  22. 22 /**
  23. 23 * 导入(导入到内存)
  24. 24 */
  25. 25 @Test
  26. 26 public void importExcel() {
  27. 27 Workbook book = null;
  28. 28 try {
  29. 29 book = Workbook.getWorkbook(new File("D:/test/test.xls"));
  30. 30 // 获得第一个工作表对象
  31. 31 Sheet sheet = book.getSheet(0);
  32. 32 int rows=sheet.getRows();
  33. 33 int columns=sheet.getColumns();
  34. 34 // 遍历每行每列的单元格
  35. 35 for(int i=0;i<rows;i++){
  36. 36 for(int j=0;j<columns;j++){
  37. 37 Cell cell = sheet.getCell(j, i);
  38. 38 String result = cell.getContents();
  39. 39 if(j==0){
  40. 40 System.out.print("姓名:"+result+" ");
  41. 41 }
  42. 42 if(j==1){
  43. 43 System.out.print("年龄:"+result+" ");
  44. 44 }
  45. 45 if((j+1)%2==0){
  46. 46 System.out.println();
  47. 47 }
  48. 48 }
  49. 49 }
  50. 50 System.out.println("========");
  51. 51 // 得到第一列第一行的单元格
  52. 52 Cell cell1 = sheet.getCell(0, 0);
  53. 53 String result = cell1.getContents();
  54. 54 System.out.println(result);
  55. 55 System.out.println("========");
  56. 56 } catch (Exception e) {
  57. 57 System.out.println(e);
  58. 58 }finally{
  59. 59 if(book!=null){
  60. 60 book.close();
  61. 61 }
  62. 62 }
  63. 63 }
  64. 64
  65. 65 /**
  66. 66 * 导出(导出到磁盘)
  67. 67 */
  68. 68 @Test
  69. 69 public void exportExcel() {
  70. 70 WritableWorkbook book = null;
  71. 71 try {
  72. 72 // 打开文件
  73. 73 book = Workbook.createWorkbook(new File("D:/test/test.xls"));
  74. 74 // 生成名为"学生"的工作表,参数0表示这是第一页
  75. 75 WritableSheet sheet = book.createSheet("学生", 0);
  76. 76 // 指定单元格位置是第一列第一行(0, 0)以及单元格内容为张三
  77. 77 Label label = new Label(0, 0, "张三");
  78. 78 // 将定义好的单元格添加到工作表中
  79. 79 sheet.addCell(label);
  80. 80 // 保存数字的单元格必须使用Number的完整包路径
  81. 81 jxl.write.Number number = new jxl.write.Number(1, 0, 30);
  82. 82 sheet.addCell(number);
  83. 83 // 写入数据并关闭文件
  84. 84 book.write();
  85. 85 } catch (Exception e) {
  86. 86 System.out.println(e);
  87. 87 }finally{
  88. 88 if(book!=null){
  89. 89 try {
  90. 90 book.close();
  91. 91 } catch (Exception e) {
  92. 92 e.printStackTrace();
  93. 93 }
  94. 94 }
  95. 95 }
  96. 96 }
  97. 97
  98. 98 /**
  99. 99 * 对象数据写入到Excel
  100. 100 */
  101. 101 @Test
  102. 102 public void writeExcel() {
  103. 103 WritableWorkbook book = null;
  104. 104 try {
  105. 105 // 打开文件
  106. 106 book = Workbook.createWorkbook(new File("D:/test/stu.xls"));
  107. 107 // 生成名为"学生"的工作表,参数0表示这是第一页
  108. 108 WritableSheet sheet = book.createSheet("学生", 0);
  109. 109
  110. 110 List<Student> stuList=queryStudentList();
  111. 111 if(stuList!=null && !stuList.isEmpty()){
  112. 112 for(int i=0; i<stuList.size(); i++){
  113. 113 sheet.addCell(new Label(0, i, stuList.get(i).getName()));
  114. 114 sheet.addCell(new Number(1, i, stuList.get(i).getAge()));
  115. 115 }
  116. 116 }
  117. 117
  118. 118 // 写入数据并关闭文件
  119. 119 book.write();
  120. 120 } catch (Exception e) {
  121. 121 System.out.println(e);
  122. 122 }finally{
  123. 123 if(book!=null){
  124. 124 try {
  125. 125 book.close();
  126. 126 } catch (Exception e) {
  127. 127 e.printStackTrace();
  128. 128 }
  129. 129 }
  130. 130 }
  131. 131
  132. 132 }
  133. 133
  134. 134 /**
  135. 135 * 读取Excel数据到内存
  136. 136 */
  137. 137 @Test
  138. 138 public void readExcel() {
  139. 139 Workbook book = null;
  140. 140 try {
  141. 141 // 打开文件
  142. 142 book = Workbook.getWorkbook(new File("D:/test/stu.xls"));
  143. 143 // 获得第一个工作表对象
  144. 144 Sheet sheet = book.getSheet(0);
  145. 145 int rows=sheet.getRows();
  146. 146 int columns=sheet.getColumns();
  147. 147 List<Student> stuList=new ArrayList<Student>();
  148. 148 // 遍历每行每列的单元格
  149. 149 for(int i=0;i<rows;i++){
  150. 150 Student stu = new Student();
  151. 151 for(int j=0;j<columns;j++){
  152. 152 Cell cell = sheet.getCell(j, i);
  153. 153 String result = cell.getContents();
  154. 154 if(j==0){
  155. 155 stu.setName(result);
  156. 156 }
  157. 157 if(j==1){
  158. 158 stu.setAge(NumberUtils.toInt(result));
  159. 159 }
  160. 160 if((j+1)%2==0){
  161. 161 stuList.add(stu);
  162. 162 stu=null;
  163. 163 }
  164. 164 }
  165. 165 }
  166. 166
  167. 167 //遍历数据
  168. 168 for(Student stu : stuList){
  169. 169 System.out.println(String.format("姓名:%s, 年龄:%s",
  170. 170 stu.getName(), stu.getAge()));
  171. 171 }
  172. 172
  173. 173 } catch (Exception e) {
  174. 174 System.out.println(e);
  175. 175 }finally{
  176. 176 if(book!=null){
  177. 177 try {
  178. 178 book.close();
  179. 179 } catch (Exception e) {
  180. 180 e.printStackTrace();
  181. 181 }
  182. 182 }
  183. 183 }
  184. 184
  185. 185 }
  186. 186
  187. 187 /**
  188. 188 * 图片写入Excel,只支持png图片
  189. 189 */
  190. 190 @Test
  191. 191 public void writeImg() {
  192. 192 WritableWorkbook wwb = null;
  193. 193 try {
  194. 194 wwb = Workbook.createWorkbook(new File("D:/test/image.xls"));
  195. 195 WritableSheet ws = wwb.createSheet("图片", 0);
  196. 196 File file = new File("D:\\test\\png.png");
  197. 197 //前两位是起始格,后两位是图片占多少个格,并非是位置
  198. 198 WritableImage image = new WritableImage(1, 4, 6, 18, file);
  199. 199 ws.addImage(image);
  200. 200 wwb.write();
  201. 201 } catch (Exception e) {
  202. 202 e.printStackTrace();
  203. 203 }finally{
  204. 204 if(wwb!=null){
  205. 205 try {
  206. 206 wwb.close();
  207. 207 } catch (Exception e) {
  208. 208 e.printStackTrace();
  209. 209 }
  210. 210 }
  211. 211 }
  212. 212 }
  213. 213
  214. 214 private List<Student> queryStudentList(){
  215. 215 List<Student> stuList=new ArrayList<Student>();
  216. 216 stuList.add(new Student("zhangsan", 20));
  217. 217 stuList.add(new Student("lisi", 25));
  218. 218 stuList.add(new Student("wangwu", 30));
  219. 219 return stuList;
  220. 220 }
  221. 221
  222. 222 public class Student {
  223. 223 private String name;
  224. 224 private int age;
  225. 225
  226. 226 public Student() {
  227. 227 }
  228. 228
  229. 229 public Student(String name, int age) {
  230. 230 super();
  231. 231 this.name = name;
  232. 232 this.age = age;
  233. 233 }
  234. 234
  235. 235 public String getName() {
  236. 236 return name;
  237. 237 }
  238. 238
  239. 239 public void setName(String name) {
  240. 240 this.name = name;
  241. 241 }
  242. 242
  243. 243 public int getAge() {
  244. 244 return age;
  245. 245 }
  246. 246
  247. 247 public void setAge(int age) {
  248. 248 this.age = age;
  249. 249 }
  250. 250 }
  251. 251 }

  以上的代码简单明了的示范了JXL的导入导出功能,具体的导入导出工具类都是在此基础上建立起来的。在最近的信用办的项目中出现了一个小问题,就是导出Excel的文件名如果是中文就会出现乱码,所以需要做一些简单的处理,如下:(实例借鉴:http://www.cnblogs.com/linjiqin/p/3540266.html

  1. response.setHeader("Content-disposition", "attachment; filename="+ new String( fileName.getBytes("gb2312"), "ISO8859-1" )+ ".xls");

 

  更加详细健壮的设置如下:

  1. fileName = new String(fileName.getBytes(),"iso-8859-1");
  2. response.setCharacterEncoding("gb2312");
  3. response.reset();
  4. response.setContentType("application/OCTET-STREAM;charset=gb2312");
  5. response.setHeader("pragma", "no-cache");
  6. response.addHeader("Content-Disposition", "attachment;filename=\""
  7. + fileName + ".xls\"");// 点击导出excle按钮时候页面显示的默认名称
  8. workbook = Workbook.createWorkbook(response.getOutputStream());

   了解这些基础知识,使用jxl导入导出Excel就轻而易举了。

版权声明:本文为jyh317原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/jyh317/p/4065906.html