因为现在再写excel2003版的比较low,所以我在这就不介绍了,直接介绍2007,我所用的编程软件是IDEA

poi操作office总共有6个jar包,在pom.xml文件中配置如下,也可下载后直接引jar包(快捷键ctrl+shift+alt+s----->modules,添加jar包即可)目前poi的jar包最高版本为3.1.6

  1. <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
  2. <dependency>
  3. <groupId>org.apache.poi</groupId>
  4. <artifactId>poi</artifactId>
  5. <version>3.16</version>
  6. </dependency>
  7. <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad -->
  8. <dependency>
  9. <groupId>org.apache.poi</groupId>
  10. <artifactId>poi-scratchpad</artifactId>
  11. <version>3.16</version>
  12. </dependency>
  13. <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
  14. <dependency>
  15. <groupId>org.apache.poi</groupId>
  16. <artifactId>poi-ooxml</artifactId>
  17. <version>3.16</version>
  18. </dependency>
  19. <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
  20. <dependency>
  21. <groupId>org.apache.poi</groupId>
  22. <artifactId>poi-ooxml-schemas</artifactId>
  23. <version>3.16</version>
  24. </dependency>
  25. <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-examples -->
  26. <dependency>
  27. <groupId>org.apache.poi</groupId>
  28. <artifactId>poi-examples</artifactId>
  29. <version>3.16</version>
  30. </dependency>
  31. <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-excelant -->
  32. <dependency>
  33. <groupId>org.apache.poi</groupId>
  34. <artifactId>poi-excelant</artifactId>
  35. <version>3.16</version>
  36. </dependency>

接下来进入正题:

1、读取excel

  1. //读取excel表格中的数据,path代表excel路径
  2. public void readExecl(String path) {
  3. try {
  4. //读取的时候可以使用流,也可以直接使用文件名
  5. XSSFWorkbook xwb = new XSSFWorkbook(path);
  6. //循环工作表sheet
  7. for (int numSheet = 0; numSheet < xwb.getNumberOfSheets(); numSheet++) {
  8. XSSFSheet xSheet = xwb.getSheetAt(numSheet);
  9. if (xSheet == null) {
  10. continue;
  11. }
  12. //循环行row
  13. for (int numRow = 0; numRow <= xSheet.getLastRowNum(); numRow++) {
  14. XSSFRow xRow = xSheet.getRow(numRow);
  15. if (xRow == null) {
  16. continue;
  17. }
  18. //循环列cell
  19. for (int numCell = 0; numCell <= xRow.getLastCellNum(); numCell++) {
  20. XSSFCell xCell = xRow.getCell(numCell);
  21. if (xCell == null) {
  22. continue;
  23. }
  24. //输出值
  25. System.out.println("excel表格中取出的数据" + getValue(xCell));
  26. }
  27. }
  28.  
  29. }
  30.  
  31. } catch (IOException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35.  
  36. /**
  37. * 取出每列的值
  38. *
  39. * @param xCell 列
  40. * @return
  41. */
  42. private String getValue(XSSFCell xCell) {
  43. if (xCell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) {
  44. return String.valueOf(xCell.getBooleanCellValue());
  45. } else if (xCell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
  46. return String.valueOf(xCell.getNumericCellValue());
  47. } else {
  48. return String.valueOf(xCell.getStringCellValue());
  49. }
  50. }

2、从数据库中取出数据表,导入并生成excel

数据库表名为Vt_User,其他注释都写得很清楚

  1. public void createExcel() {
  2. try {
  3. String path = "D:/test.xlsx";
  4. // 创建新的Excel 工作簿
  5. XSSFWorkbook workbook = new XSSFWorkbook();
  6. // 在Excel工作簿中建一工作表,其名为缺省值
  7. // 如要新建一名为"用户表"的工作表,其语句为:
  8. XSSFSheet sheet = workbook.createSheet("用户表");
  9. // 在索引0的位置创建行(最顶端的行)
  10. XSSFRow row = sheet.createRow((short) 0);
  11. //在索引0的位置创建单元格(左上端)
  12. XSSFCell cell = row.createCell((short) 0);
  13. //创建单元格样式
  14. CellStyle cellStyle = workbook.createCellStyle();
  15. // 设置这些样式
  16. cellStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
  17. cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
  18. cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
  19. cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
  20. cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
  21. cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
  22. cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
  23.  
  24. // 定义单元格为字符串类型
  25. cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  26. // 在单元格中输入一些内容
  27. cell = row.createCell((short) 0);
  28. cell.setCellValue("用户id");
  29. cell.setCellStyle(cellStyle);
  30.  
  31. cell = row.createCell((short) 1);
  32. cell.setCellValue("姓名");
  33. cell.setCellStyle(cellStyle);
  34.  
  35. cell = row.createCell((short) 2);
  36. cell.setCellValue("别名");
  37. cell.setCellStyle(cellStyle);
  38.  
  39. cell = row.createCell((short) 3);
  40. cell.setCellValue("密码");
  41. cell.setCellStyle(cellStyle);
  42.  
  43. cell = row.createCell((short) 4);
  44. cell.setCellValue("外来id");
  45. cell.setCellStyle(cellStyle);
  46.  
  47. //查询数据库中所有的数据
  48. VtUserMapper mapper = getMapper(VtUserMapper.class);
  49. VtUserCriteria cri = new VtUserCriteria();
  50. cri.createCriteria().andUserEnabledEqualTo(1);
  51. List<VtUser> list = mapper.selectByExample(cri);
  52. /*//第一个sheet第一行为标题
  53. XSSFRow rowFirst = sheet.createRow(0);
  54. rowFirst.setHeightInPoints(21.75f);*/
  55. for (int i = 0; i < list.size(); i++) {
  56. row = sheet.createRow((int) i + 1);
  57. VtUser stu = (VtUser) list.get(i);
  58. // 第四步,创建单元格,并设置值
  59. row.createCell((short) 0).setCellValue(stu.getUserId());
  60. row.createCell((short) 1).setCellValue(stu.getUserName());
  61. row.createCell((short) 2).setCellValue(stu.getUserNameZn());
  62. row.createCell((short) 3).setCellValue(stu.getUserPassword());
  63. row.createCell((short) 4).setCellValue(stu.getUserForeignId());
  64. sheet.autoSizeColumn((short) 0); //调整第一列宽度(自适应),只识别数字、字母
  65. sheet.autoSizeColumn((short) 1); //调整第二列宽度
  66. //调整第三列宽度,有中文,先判断这一列的最长字符串
  67. int length = stu.getUserNameZn().getBytes().length;
  68. sheet.setColumnWidth((short)2,(short)(length*2*256));
  69. sheet.autoSizeColumn((short) 3); //调整第四列宽度
  70. sheet.autoSizeColumn((short) 4); //调整第五列宽度
  71.  
  72. /*Font font = workbook.createFont();
  73. font.setFontHeightInPoints((short)18); //字体大小
  74. sheet.setDefaultRowHeightInPoints(21.75f);
  75. font.setFontName("楷体");
  76. font.setBoldweight(Font.BOLDWEIGHT_BOLD); //粗体
  77. font.setColor(HSSFColor.GREEN.index); //绿字- 字体颜色*/
  78. }
  79. // 新建一输出文件流
  80. FileOutputStream fOut = new FileOutputStream(path);
  81. // 把相应的Excel 工作簿存盘
  82. workbook.write(fOut);
  83. //清空缓冲区数据
  84. fOut.flush();
  85. // 操作结束,关闭文件
  86. fOut.close();
  87. System.out.println("文件生成...");
  88. } catch (Exception e) {
  89. System.out.println("已运行 xlCreate() : " + e);
  90. }
  91. }

3、修改excel

  1. //修改excel表格,path为excel修改前路径(D:\\test.xlsx)
  2. public void writeExcel3(String path) {
  3. try {
  4. //传入的文件
  5. FileInputStream fileInput = new FileInputStream(path);
  6. //poi包下的类读取excel文件
  7.  
  8. // 创建一个webbook,对应一个Excel文件
  9. XSSFWorkbook workbook = new XSSFWorkbook(fileInput);
  10. //对应Excel文件中的sheet,0代表第一个
  11. XSSFSheet sh = workbook.getSheetAt(0);
  12. //修改excle表的第5行,从第三列开始的数据
  13. for (int i = 2; i < 4; i++) {
  14. //对第五行的数据修改
  15. sh.getRow(4).getCell((short) i).setCellValue(100210 + i);
  16. }
  17. //将修改后的文件写出到D:\\excel目录下
  18. FileOutputStream os = new FileOutputStream("D:\\修改后test.xlsx");
  19. // FileOutputStream os = new FileOutputStream("D:\\test.xlsx");//此路径也可写修改前的路径,相当于在原来excel文档上修改
  20. os.flush();
  21. //将Excel写出
  22. workbook.write(os);
  23. //关闭流
  24. fileInput.close();
  25. os.close();
  26. } catch (IOException e) {
  27. e.printStackTrace();
  28. }
  29. }

poi操作excel2007(读取、生成、编辑)的更多相关文章

  1. POI操作Excel2007实例二之“SXSSFWorkbook”处理海量数据

    转自:http://blog.csdn.net/little_stars/article/details/8266262 前文讲述了 POI 读取的基本操作,但后期 经过试验,当写入数据量超过5万条以 ...

  2. Java POI 操作Excel(读取/写入)

    pom.xml依赖: <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi< ...

  3. java poi 操作

    Java POI 操作Excel(读取/写入) https://www.cnblogs.com/dzpykj/p/8417738.html Java操作Excel之Poi基本操作 https://my ...

  4. POI操作Excel详解,读取xls和xlsx格式的文件

    package org.ian.webutil;   import java.io.File; import java.io.FileInputStream; import java.io.FileN ...

  5. Java使用poi从数据库读取数据生成Excel表格

    想要使用POI操作以xsl结尾的Excel,首先要下载poi相关的jar包,用到的jar有: poi-3.9.jar poi-ooxml-3.9.jar poi-ooxml-schemas-3.9.j ...

  6. POI操作Excel

    POI和Excel简介 JAVA中操作Excel的有两种比较主流的工具包: JXL 和 POI .jxl 只能操作Excel 95, 97, 2000也即以.xls为后缀的excel.而poi可以操作 ...

  7. java使用Apache POI操作excel文件

    官方介绍 HSSF is the POI Project's pure Java implementation of the Excel '97(-2007) file format. XSSF is ...

  8. 使用POI操作Excel时对事先写入模板的公式强制执行

    场景:POI读取Excel模板. 当使用POI操作Excel时,发现由POI生成的公式能够在打开Excel是被执行, 而事先手工写入Excel模板文件的公式则不自动被调用,必须手动双击该Cell才能生 ...

  9. 全面了解POI操作Microsoft Office(Word、Excel、PowerPoint)

    POI 与 Microsoft Office 1. POI 简介 POI 是 Apache 下的 Jakata 项目的一个子项目,主要用于提供 java 操作 Microsoft Office 办公套 ...

随机推荐

  1. idea debug打得断点第一次可以进入,第二次不能进入

  2. yii 正则验证

    required : 必须值验证属性 [['字段名'],required,'requiredValue'=>'必填值','message'=>'提示信息']; #说明:CRequiredV ...

  3. [HTML5] Semantics for accessibility

    For example, when we use checkbox, if we do like this: <div class="inline-control sign-up co ...

  4. Web前端开发实战2:二级下拉式菜单之JS实现

    上一篇博文提到了二级下拉式菜单是用HTML和CSS实现的.我们这一篇来用JavaScript脚本实现下拉菜单的显 示和隐藏. 使用 JavaScript方法实现我们须要用的知识有: 1)JS事件:on ...

  5. HTML5 Canvas 获取网页的像素值。

    我之前在网上看过一个插件叫做出JScolor   颜色拾取器  说白了就是通过1*1PX的DOM设置颜色值通过JS来获取当前鼠标点击位置DOM的颜色值. 自从HTML5 画布出来之后.就有更好的方法来 ...

  6. Cocos2d-x3.0 RenderTexture(三)

    .h #include "cocos2d.h" #include "cocos-ext.h" #include "ui/CocosGUI.h" ...

  7. Codeforces Beta Round #29 (Div. 2, Codeforces format) C. Mail Stamps 拓扑排序

    C. Mail Stamps     One day Bob got a letter in an envelope. Bob knows that when Berland's post offic ...

  8. iOS~判断应用是否有定位权限

    在特定场景下我们需要判断用户是否允许应用获取定位权限 1.导入类库: #import <CoreLocation/CLLocationManager.h> 2.判断用户手机是否开启了定位服 ...

  9. 【HDU 5015】233 Matrix

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5015 [算法] 矩阵乘法 [代码] #include<bits/stdc++.h> u ...

  10. SQL语句之transaction

    http://blog.csdn.net/xbc1213/article/details/50169935 案例: begin tran --定义一个存储错误新的变量 执行一条语句 set @sumE ...