java 操作Excel

JxlDemo.java

package org.zbq.execl;

import java.io.File;
import java.io.IOException;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableCell;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class JxlDemo {
    public void write(String fileName) throws IOException, RowsExceededException, WriteException {
        File file = new File(fileName);
        WritableWorkbook workbook = Workbook.createWorkbook(file);

        WritableSheet sheet = workbook.createSheet("sheet1", 0);
        for(int row=0; row<10; row++) {
            for(int col=0; col<10; col++) {
                WritableCell cell = new Label(col, row, "date"+row+col);
                sheet.addCell(cell);
            }
        }

        workbook.write();
        workbook.close();
    }

    public void read(String fileName) throws BiffException, IOException {
        File file = new File(fileName);
        Workbook workbook = Workbook.getWorkbook(file);
        Sheet[]  sheets = workbook.getSheets();

        Sheet sheet_0 = sheets[0];

        int rows = sheet_0.getRows();
        int cols = sheet_0.getColumns();

        for(int row=0; row<rows; row++) {
            for(int col=0; col<cols; col++) {
                System.out.printf("%10s", sheet_0.getCell(row, col).getContents());
            }
            System.out.println();
        }

        workbook.close();
    }


}

PoiDemo.java

package org.zbq.execl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.EncryptedDocumentException;
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.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;



public class PoiDemo {

    public void write(String fileName) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("sheet1");

        for(int row=0; row<10; row++) {
            HSSFRow rownum = sheet.createRow(row);
            for(int col=0; col<10; col++) {
                rownum.createCell(col).setCellValue("data" + row + col);
            }
        }

        File xlsFile = new File(fileName);
        FileOutputStream xlsStream = new FileOutputStream(xlsFile);
        workbook.write(xlsStream);
        workbook.close();
    }

    public void read(String fileName) throws EncryptedDocumentException, InvalidFormatException, IOException {
        File xlsFile = new File(fileName);
        Workbook workbook = WorkbookFactory.create(xlsFile);

        Sheet sheet_0 = workbook.getSheetAt(0);

        int rows = sheet_0.getLastRowNum() + 1;
        Row tmp = sheet_0.getRow(0);

        int cols = tmp.getPhysicalNumberOfCells();
        for(int row=0; row<rows; row++) {
            Row r = sheet_0.getRow(row);
            for(int col=0; col<cols; col++) {
                System.out.printf("%10s", r.getCell(col).getStringCellValue());
            }
            System.out.println();
        }
    }
}

Main.java

package org.zbq.execl;

import java.io.IOException;

import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

import jxl.read.biff.BiffException;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class Main {

    public static void main(String[] args) throws RowsExceededException, WriteException, IOException, BiffException, EncryptedDocumentException, InvalidFormatException {
        System.out.println("ssss");
        JxlDemo jxl = new JxlDemo();
        String fileName = "abc.xlsx";
//      demo.write(fileName);

        String filein = "binqiang.xlsx";
//      jxl.read(filein);

        PoiDemo poi = new PoiDemo();
//      poi.write(fileName);

        poi.read("binqiang.xlsx");
    }

}

JXL 好像对2007版本的好像 不能读

maven pom.xml

<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>org.zbq</groupId>
    <artifactId>excel</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.hynnet</groupId>
            <artifactId>jxl</artifactId>
            <version>2.6.12.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.16</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.16</version>
        </dependency>
    </dependencies>
</project>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值