https://mirrors.cnnic.cn/apache/poi/xmlbeans/release/src/

  1. package Excel;
  2.  
  3. import org.apache.poi.hssf.usermodel.HSSFCell;
  4. import org.apache.poi.hssf.usermodel.HSSFRow;
  5. import org.apache.poi.hssf.usermodel.HSSFSheet;
  6. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  7. import org.apache.poi.ss.usermodel.CellType;
  8. import org.apache.poi.xssf.usermodel.XSSFCell;
  9. import org.apache.poi.xssf.usermodel.XSSFRow;
  10. import org.apache.poi.xssf.usermodel.XSSFSheet;
  11. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  12.  
  13. import java.io.File;
  14. import java.io.FileNotFoundException;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import java.text.SimpleDateFormat;
  18. import java.util.Calendar;
  19. import java.util.Date;
  20.  
  21. public class WriteExcel {
  22.  
  23. Date dt = new Date();
  24. SimpleDateFormat format = new SimpleDateFormat("YYYYMMddHHmmss");
  25. String time = format.format(dt);
  26.  
  27. public void WriteExcelxls() {
  28.  
  29. FileOutputStream out = null;
  30. try {
  31. out = new FileOutputStream(new File(".\\Log\\旧的EXCEL文件_"+time+".xls"));
  32. HSSFWorkbook workxls = new HSSFWorkbook();
  33. HSSFSheet sheet = workxls.createSheet(time);
  34. HSSFRow row = workxls.getSheet(time).createRow(0);
  35.  
  36. for (short i = 0; i < 10; i++) {
  37. HSSFCell cell = row.createCell(i);
  38. cell.setCellValue("测试" + i);
  39. }
  40.  
  41. sheet.createRow(1).createCell(1).setCellValue("1234567890");
  42. sheet.createRow(2).createCell(0).setCellValue(Calendar.getInstance());
  43. sheet.createRow(3).createCell(0).setCellValue("字符串");
  44. sheet.createRow(4).createCell(0).setCellValue(true);
  45. sheet.createRow(5).createCell(0).setCellType(CellType.ERROR);
  46. workxls.write(out);
  47. out.close();
  48. System.out.println("旧的EXCEL文件_.xls written successfully on disk.");
  49. } catch (FileNotFoundException e) {
  50. e.printStackTrace();
  51. } catch (IOException e) {
  52. e.printStackTrace();
  53. }
  54.  
  55. }
  56.  
  57. public void WriteExcelxlsx() {
  58.  
  59. File file = new File(".\\Log\\新的EXCEL文件_"+time+".xlsx");
  60. FileOutputStream out = null;
  61. try {
  62. out = new FileOutputStream(file);
  63. XSSFWorkbook workbook = new XSSFWorkbook();
  64. XSSFRow row = workbook.createSheet(time).createRow(0);
  65. XSSFSheet sheet = workbook.getSheet(time);
  66.  
  67. for (short i = 0; i < 10; i++) {
  68. XSSFCell cell = row.createCell(i);
  69. cell.setCellValue("新的EXCEL文件" + i);
  70. }
  71.  
  72. sheet.createRow(1).createCell(1).setCellValue("1234567890");
  73. sheet.createRow(2).createCell(0).setCellValue(Calendar.getInstance());
  74. sheet.createRow(3).createCell(0).setCellValue("字符串");
  75. sheet.createRow(4).createCell(0).setCellValue(true);
  76. sheet.createRow(5).createCell(0).setCellType(CellType.ERROR);
  77. workbook.write(out);
  78. out.close();
  79. System.out.println("新的EXCEL文件_.xlsx written successfully on disk.");
  80.  
  81. } catch (FileNotFoundException e) {
  82. e.printStackTrace();
  83. } catch (IOException e) {
  84. e.printStackTrace();
  85. }
  86. }
  87.  
  88. }

  

  1. package Excel;
  2.  
  3. import org.testng.annotations.Test;
  4.  
  5. public class TestExcel {
  6.  
  7. @Test(priority = 1)
  8. private void Testold()
  9. {
  10. WriteExcel aaa = new WriteExcel();
  11. aaa.WriteExcelxls();
  12.  
  13. }
  14.  
  15. @Test(priority = 2)
  16. public void Testnew()
  17. {
  18. WriteExcel aaa = new WriteExcel();
  19. aaa.WriteExcelxlsx();
  20.  
  21. }
  22. }

  依赖:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6.  
  7. <groupId>Jasmine</groupId>
  8. <artifactId>Test</artifactId>
  9. <version>1.0-SNAPSHOT</version>
  10.  
  11. <dependencies>
  12. <dependency>
  13. <groupId>org.apache.poi</groupId>
  14. <artifactId>poi</artifactId>
  15. <version>4.0.0</version>
  16. </dependency>
  17. <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
  18. <dependency>
  19. <groupId>org.apache.poi</groupId>
  20. <artifactId>poi-ooxml</artifactId>
  21. <version>4.0.0</version>
  22. </dependency>
  23. <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
  24. <dependency>
  25. <groupId>org.apache.poi</groupId>
  26. <artifactId>poi-ooxml-schemas</artifactId>
  27. <version>4.0.0</version>
  28. </dependency>
  29. <!-- https://mvnrepository.com/artifact/org.testng/testng -->
  30. <dependency>
  31. <groupId>org.testng</groupId>
  32. <artifactId>testng</artifactId>
  33. <version>6.9.4</version>
  34. <scope>test</scope>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.testng</groupId>
  38. <artifactId>testng</artifactId>
  39. <version>6.13.1</version>
  40. </dependency>
  41. </dependencies>
  42. </project>

  

  1. package Excel;
  2.  
  3. import org.apache.poi.hssf.usermodel.*;
  4. import org.apache.poi.ss.usermodel.CellType;
  5.  
  6. import java.io.File;
  7. import java.io.FileNotFoundException;
  8. import java.io.FileOutputStream;
  9. import java.io.IOException;
  10. import java.text.SimpleDateFormat;
  11. import java.util.Calendar;
  12. import java.util.Date;
  13.  
  14. public class WriteExcelMore {
  15.  
  16. static Date dt = new Date();
  17. static SimpleDateFormat format = new SimpleDateFormat("YYYYMMddHHmmss");
  18. static String time = format.format(dt);
  19.  
  20. public static void main(String args[]) {
  21.  
  22. File file = new File(".\\Log\\新的EXCEL文件_" + time + ".xls");
  23. FileOutputStream out = null;
  24. try {
  25. out = new FileOutputStream(file);
  26. HSSFWorkbook workbook = new HSSFWorkbook(); //创建一个新的excel
  27. HSSFSheet sheet = workbook.createSheet(time); //创建sheet页
  28. HSSFHeader header = sheet.getHeader();//创建header页
  29. header.setCenter("Title");
  30.  
  31. HSSFRow[] row = new HSSFRow[3];
  32. row[0] = sheet.createRow(0);
  33. HSSFCell headerCell = row[0].createCell(5);
  34. headerCell.setCellValue(new HSSFRichTextString("标题"));
  35. //HSSFRow row = workbook.getSheet(time).createRow(3);
  36. row[1] = sheet.createRow(1);
  37.  
  38. for (short i = 0; i < 5; i++) {
  39. HSSFCell cell = row[1].createCell(i);
  40. cell.setCellValue("新的EXCEL文件" + i);
  41. }
  42.  
  43. row[2] = sheet.createRow(2);
  44. String[] arr = new String[5];
  45. String[] arr2 = {"aa", "bb", "cc", "dd", "ee"};
  46. for (short i = 0; i < 5; i++) {
  47. HSSFCell cell = row[2].createCell(i);
  48. cell.setCellValue(arr2[i]);
  49. }
  50.  
  51. sheet.createRow(5).createCell(1).setCellValue("1234567890");
  52. sheet.createRow(6).createCell(0).setCellValue(Calendar.getInstance());
  53. sheet.createRow(7).createCell(0).setCellValue("字符串");
  54. sheet.createRow(8).createCell(0).setCellValue(true);
  55. sheet.createRow(9).createCell(0).setCellType(CellType.ERROR);
  56.  
  57. //设置footer
  58. sheet.setGridsPrinted(false);
  59. HSSFFooter footer = sheet.getFooter();
  60. footer.setRight("page " + HeaderFooter.page() + "of" + HeaderFooter.numPages());
  61.  
  62. workbook.write(out);
  63. out.close();
  64. System.out.println(file + " written successfully on disk.");
  65.  
  66. } catch (FileNotFoundException e) {
  67. e.printStackTrace();
  68. } catch (IOException e) {
  69. e.printStackTrace();
  70. }
  71. }
  72.  
  73. }

  

遇到的错如下:

1. Exception in thread "main" java.lang.NoClassDefFoundError:

org/dom4j/DocumentExceptionCaused by: java.lang.ClassNotFoundException: org.dom4j.DocumentException

2. Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/compress/archivers/zip/ZipFile

3. Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/collections4/ListValuedMap

4. Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject

5. java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlException

<!-- https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans -->
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>3.0.0</version>
</dependency>

6. java.lang.NoClassDefFoundError: org/apache/commons/collections4/ListValuedMap

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.1</version>
</dependency>

7. java.lang.NoClassDefFoundError: org/apache/commons/compress/archivers/zip/ZipFile

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-compress -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.18</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>

8. java.lang.NoSuchMethodError: org.apache.xmlbeans.XmlOptions.setEntityExpansionLimit(I)Lorg/apache/xmlbeans/XmlOptions;

上述都是包的问题,后来新建了一个项目,换成maven管理依赖包,一次性解决了。

update 20181122

将上述代码拖到UI自动化项目中,发现执行报各种错误,已添加到上面。 

都是jar包的问题,但是我已经将测试成功项目中的pom文件中的jar包都下载来了,却还是报各种错,解决了一个又遇到下一个。 (┬_┬)

maven打包,因为网络问题,有时候有的包未能下载下来,就自己下载jar包引用,可是(┬_┬)(┬_┬)无力

苦心人天不负,三千越界可吞吴。哈哈哈哈,在尝试了千万遍之后,终于成功了,下面是所有新引入的jar包,百度网盘链接如下:

其中红框框是为了解决excel中xlsx格式而引入的一系列jar包。

链接:https://pan.baidu.com/s/1JhKENJU1PLlgOwyrnptilg
提取码:ip2u
链接若失效,请联系我

Java 打开Excel,往Excel中存入值,保存的excel格式分别是xls和xlsx的更多相关文章

  1. Java如何获取JSON数据中的值

    场景:在接口自动化场景中,下个接口发送的请求参数,依赖上个接口请求结果中的值.需要将获取值作为全局参数引用. import java.io.File; import java.io.FileInput ...

  2. java引用数据类型在方法中的值传递

    package org.jimmy.autosearch20180821.test; public class TestStringArr { public static void main(Stri ...

  3. java 基础:方法调用中的值传递是call by value,并且传递的是参数的值的拷贝,而不是引用

    public class TestExtends { public static void main(String[]args){ int s = 10; System.out.println(Sys ...

  4. 一个简单java爬虫爬取网页中邮箱并保存

    此代码为一十分简单网络爬虫,仅供娱乐之用. java代码如下: package tool; import java.io.BufferedReader; import java.io.File; im ...

  5. 将table中的值转换成json格式传到后台接收处理。

    table数据 <table style="border:1px" id="tableID"> <tr> <th>编号< ...

  6. 用SQL语句将数据表中的数据保存为JSON格式

    没有找到好的工具,只想到了拼字符串的方式,用   NVARCHAR(MAX)  可能有截断,不推荐使用,方法中使用了 FOR XML PATH('') 实现,有关其使用方法参考这里 表结构: SQL ...

  7. C# 导出dataGridView中的值到Excel

    C# 怎么导出dataGridView中的值到Excel 1 2 3 4 5 6 在系统应用过程中,数据是系统的核心.如果直接在应用软件中看数据,有时也有些不便,所以就会把系统数据转换成Excel格式 ...

  8. POI操作Excel(xls、xlsx)

    阿帕奇官网:http://poi.apache.org/ POI3.17下载:http://poi.apache.org/download.html#POI-3.17 POI操作Excel教程(易百教 ...

  9. struts框架问题六之从值栈中获取值

    6. 问题六: 在JSP中获取值栈的数据 * 总结几个小问题: > 访问root中数据 不需要# > 访问context其它对象数据 加 # > 如果向root中存入对象的话,优先使 ...

随机推荐

  1. [LeetCode] 724. Find Pivot Index_Easy tag: Dynamic Programming

    Given an array of integers nums, write a method that returns the "pivot" index of this arr ...

  2. [LeetCode] 122. Best Time to Buy and Sell Stock II_Easy tag: Dynamic Programming

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  3. java实现Comparable接口和Comparator接口,并重写compareTo方法和compare方法

    原文地址https://segmentfault.com/a/1190000005738975 实体类:java.lang.Comparable(接口) + comareTo(重写方法),业务排序类 ...

  4. recv函数返回值说明

    recv函数 int recv( SOCKET s, char FAR *buf, int len, int flags); 不论是客户还是服务器应用程序都用recv函数从TCP连接的另一端接收数据. ...

  5. php发送 与接收流文件

    PHP 发送与接收流文件 sendStreamFile.php 把文件以流的形式发送 receiveStreamFile.php 接收流文件并保存到本地 sendStreamFile.php < ...

  6. c#: using Microsoft.Office.Interop.Excel 异常

    解决方法: Project>Reference>右键Add Reference...>Choose Microsoft Excel 15.0 Object Library

  7. Q-learning简明实例

    本文是对 http://mnemstudio.org/path-finding-q-learning-tutorial.htm 的翻译,共分两部分,第一部分为中文翻译,第二部分为英文原文.翻译时为方便 ...

  8. Thinkphp中查询复杂sql查询表达式,如何表达MYSQL中的某字段不为空is not null?

    Thinkphp中查询复杂sql查询表达式,如何表达MYSQL中的某字段不为空is not null?先上两种实现方式的实例:$querys["house_type_image"] ...

  9. Web负载均衡学习笔记之四层和七层负载均衡的区别

    0x00 简介 简单理解四层和七层负载均衡: ① 所谓四层就是基于IP+端口的负载均衡:七层就是基于URL等应用层信息的负载均衡:同理,还有基于MAC地址的二层负载均衡和基于IP地址的三层负载均衡. ...

  10. Js删除字符串中的指定字符串

    案例一. 比如:原字符串 var StringFirst = "12:30:08"; 现在要删掉冒号,变成123008 就可以先split var splitFirst = Str ...