sa

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>

  

遇到的错如下:

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

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

猜你喜欢

转载自www.cnblogs.com/qianjinyan/p/9758395.html
sa