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

package Excel;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date; public class WriteExcel { Date dt = new Date();
SimpleDateFormat format = new SimpleDateFormat("YYYYMMddHHmmss");
String time = format.format(dt); public void WriteExcelxls() { FileOutputStream out = null;
try {
out = new FileOutputStream(new File(".\\Log\\旧的EXCEL文件_"+time+".xls"));
HSSFWorkbook workxls = new HSSFWorkbook();
HSSFSheet sheet = workxls.createSheet(time);
HSSFRow row = workxls.getSheet(time).createRow(0); for (short i = 0; i < 10; i++) {
HSSFCell cell = row.createCell(i);
cell.setCellValue("测试" + i);
} sheet.createRow(1).createCell(1).setCellValue("1234567890");
sheet.createRow(2).createCell(0).setCellValue(Calendar.getInstance());
sheet.createRow(3).createCell(0).setCellValue("字符串");
sheet.createRow(4).createCell(0).setCellValue(true);
sheet.createRow(5).createCell(0).setCellType(CellType.ERROR);
workxls.write(out);
out.close();
System.out.println("旧的EXCEL文件_.xls written successfully on disk.");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} } public void WriteExcelxlsx() { File file = new File(".\\Log\\新的EXCEL文件_"+time+".xlsx");
FileOutputStream out = null;
try {
out = new FileOutputStream(file);
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFRow row = workbook.createSheet(time).createRow(0);
XSSFSheet sheet = workbook.getSheet(time); for (short i = 0; i < 10; i++) {
XSSFCell cell = row.createCell(i);
cell.setCellValue("新的EXCEL文件" + i);
} sheet.createRow(1).createCell(1).setCellValue("1234567890");
sheet.createRow(2).createCell(0).setCellValue(Calendar.getInstance());
sheet.createRow(3).createCell(0).setCellValue("字符串");
sheet.createRow(4).createCell(0).setCellValue(true);
sheet.createRow(5).createCell(0).setCellType(CellType.ERROR);
workbook.write(out);
out.close();
System.out.println("新的EXCEL文件_.xlsx written successfully on disk."); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} }

  

package Excel;

import org.testng.annotations.Test;

public class TestExcel {

    @Test(priority = 1)
private void Testold()
{
WriteExcel aaa = new WriteExcel();
aaa.WriteExcelxls(); } @Test(priority = 2)
public void Testnew()
{
WriteExcel aaa = new WriteExcel();
aaa.WriteExcelxlsx(); }
}

  依赖:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>Jasmine</groupId>
<artifactId>Test</artifactId>
<version>1.0-SNAPSHOT</version> <dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.13.1</version>
</dependency>
</dependencies>
</project>

  

package Excel;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.CellType; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date; public class WriteExcelMore { static Date dt = new Date();
static SimpleDateFormat format = new SimpleDateFormat("YYYYMMddHHmmss");
static String time = format.format(dt); public static void main(String args[]) { File file = new File(".\\Log\\新的EXCEL文件_" + time + ".xls");
FileOutputStream out = null;
try {
out = new FileOutputStream(file);
HSSFWorkbook workbook = new HSSFWorkbook(); //创建一个新的excel
HSSFSheet sheet = workbook.createSheet(time); //创建sheet页
HSSFHeader header = sheet.getHeader();//创建header页
header.setCenter("Title"); HSSFRow[] row = new HSSFRow[3];
row[0] = sheet.createRow(0);
HSSFCell headerCell = row[0].createCell(5);
headerCell.setCellValue(new HSSFRichTextString("标题"));
//HSSFRow row = workbook.getSheet(time).createRow(3);
row[1] = sheet.createRow(1); for (short i = 0; i < 5; i++) {
HSSFCell cell = row[1].createCell(i);
cell.setCellValue("新的EXCEL文件" + i);
} row[2] = sheet.createRow(2);
String[] arr = new String[5];
String[] arr2 = {"aa", "bb", "cc", "dd", "ee"};
for (short i = 0; i < 5; i++) {
HSSFCell cell = row[2].createCell(i);
cell.setCellValue(arr2[i]);
} sheet.createRow(5).createCell(1).setCellValue("1234567890");
sheet.createRow(6).createCell(0).setCellValue(Calendar.getInstance());
sheet.createRow(7).createCell(0).setCellValue("字符串");
sheet.createRow(8).createCell(0).setCellValue(true);
sheet.createRow(9).createCell(0).setCellType(CellType.ERROR); //设置footer
sheet.setGridsPrinted(false);
HSSFFooter footer = sheet.getFooter();
footer.setRight("page " + HeaderFooter.page() + "of" + HeaderFooter.numPages()); workbook.write(out);
out.close();
System.out.println(file + " written successfully on disk."); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} }

  

遇到的错如下:

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. 申请 Let’s Encrypt 泛域名证书 及 Nginx/Apache 证书配置

    什么是 Let’s Encrypt? 部署 HTTPS 网站的时候需要证书,证书由 CA (Certificate Authority )机构签发,大部分传统 CA 机构签发证书是需要收费的,这不利于 ...

  2. Js中split()方法的正确使用

    通过 js 获取 QueryString (location.search部分) 参数很常见,网上代码也满天飞.不过现在的框架,基本上都通过路由伪静态了,把以前的 QueryString 变成了pat ...

  3. cxf-webservice完整示例

    最近一段时间研究webservice,一般来说,开发java的Webservice经常使用axis2和cxf这两个比较流行的框架 先使用cxf,开发一个完整示例,方便对webservice有一个整体的 ...

  4. UNIX历史

    一.Multics计划 1965年,AT&T贝尔电话实验室.通用电气公司.麻省理工学院MAC课题组一起联合开发一个称为Multics的新操作系统. Multics 系统的目标是要向大的用户团体 ...

  5. CAEAGLLayer

    CAEAGLLayer 当iOS要处理高性能图形绘制,必要时就是OpenGL.应该说它应该是最后的杀手锏,至少对于非游戏的应用来说是的.因为相比Core Animation和UIkit框架,它不可思议 ...

  6. Java SE 基础知识(二)

    1. 类由两大部分构成:属性和方法.属性一般用名词来表示,方法一般用动词来表示. 2. 如果一个java源文件中定义了多个类,那么这些类中最多只能有一个类是public的,可以都不是public的. ...

  7. Linux服务器---配置apache支持php

    apache支持php php是最好用的服务器语言了,Apache对php有很强大的支持 1.检测是否安装php,如果什么信息也没有,那么你就要自己安装php了 [root@localhost ~]# ...

  8. Window下PHP三种运行方式图文详解,window下的php是不是单进程的?

    Window下PHP三种运行方式图文详解,window下的php是不是单进程的? PHP运行目前为止主要有三种方式: a.以模块加载的方式运行,初学者可能不容易理解,其实就是将PHP集成到Apache ...

  9. python3.4学习笔记(二十一) python实现指定字符串补全空格、前面填充0的方法

    python3.4学习笔记(二十一) python实现指定字符串补全空格.前面填充0的方法 Python zfill()方法返回指定长度的字符串,原字符串右对齐,前面填充0.zfill()方法语法:s ...

  10. 一个快速检测系统CPU负载的小程序

    原理说明 在对服务器进行维护时,有时也遇到由于系统 CPU(利用率)负载过高导致业务中断的情况.服务器上可能运行多个进程,查看单个进程的 CPU 都是正常的,但是整个系统的 CPU 负载可能是异常的. ...