表格操作

本文对比了jxl与poi在Excel处理上的差异,并指出jxl存在版本限制且不支持doc文件。提供了使用jxl进行Excel读写操作的具体代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

感觉现在poi用的比jxl较多,jxl相对过时,本人在操作过程中,发现jxl不能操作doc文件,以及只适用于版本比较低的excel,郁闷。虽然没有比对jxl和poi的性能,但是对于现在的计算机性能,感觉也差不了多少(说话很不负责任)。后期要考虑转入poi阵营

转载了一篇关于pio和jxl的性能对比博客:
http://blog.youkuaiyun.com/Demodan/article/details/78774621

下面给出的是关于jxl的简单操作:

package com.zd.excel.demo;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.CellFormat;
import jxl.read.biff.BiffException;
import jxl.write.*;

import java.io.*;

/**
 *  表格工具类
 * 1.创建excel表
 * 2.读取excel表
 *
 * Created by ZD on 2017/12/11.
 */
public class ExcelUtils {

    /**
     * 读取文档内容,将其写入excel表格中
     * 行读取
     */
    public static void writeExcel(File file) throws IOException, WriteException {

        //1,创建workbook
        WritableWorkbook workbook = Workbook.createWorkbook(new File("test.xls"));

        //2. 创建sheet
        WritableSheet sheet = workbook.createSheet("全国地区代码",0);

        //3. 设置表标题字体大小
        WritableFont writableFont = new WritableFont(WritableFont.ARIAL,20,WritableFont.BOLD);

        //4. 设置表标题单元格格式
        CellFormat cellFormat = new WritableCellFormat(writableFont);

        //5.创建label,用于存放标题
        Label header = new Label(0,0,"全国地区代码",cellFormat);
        sheet.addCell(header);

        //6.创建表头
        Label id = new Label(0,1,"编号",cellFormat);
        sheet.addCell(id);
        Label code = new Label(1,1,"代码",cellFormat);
        sheet.addCell(code);
        Label name = new Label(2,1,"名称",cellFormat);
        sheet.addCell(name);

        //7.读取文件,写入表格
       // File file = new File("test.txt");
        if (!file.exists())
            return;

        //乱码问题
        InputStream inputStream = new FileInputStream(file);
        InputStreamReader fileReader = new InputStreamReader(inputStream,"GBK");
       // FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);


        String line;
        Label label = null;

        //如果输入流读取到文档文本行
        for (int i = 1; (line = bufferedReader.readLine())!=null; i++){

            line = line.trim();

            //拆分字符串,如果读取的行中不包含有数值格式,则返回本次循环
            if (!line.matches("^[0-9].*")) {
                //结束本次循环
                i--;
                continue;
            }

            //拆分字符串-地区代码
            String icode = line.substring(0,4);

            //拆分字符串-地区名称
            String[] names = line.split("[0-9]*");
            StringBuilder iname = new StringBuilder();
            for (int z = 0; z < names.length; z++){
                if (names[z].equals("")||names[z].equals("\t"))
                    continue;
                iname.append(names[z]);
            }
            //加入编号
            label = new Label(0,i+1,i+"");
            sheet.addCell(label);

            //加入地区代码
            label = new Label(1,i+1,icode.toString());
            sheet.addCell(label);
            //加入地区名称
            label = new Label(2,i+1,iname.toString());
            sheet.addCell(label);
        }
        workbook.write();
        bufferedReader.close();
        fileReader.close();
        workbook.close();
    }

    /**
     * 从excel表中读取信息,将其写入txt中
     * @param file
     * @throws IOException
     * @throws BiffException
     */
    private static void readExcel(File file) throws IOException, BiffException {
        //1.创建workbook
        Workbook workbook = Workbook.getWorkbook(file);

        //2.获取sheet
        Sheet sheet = workbook.getSheet(0);

        //3.获取所有行数
        int rows = sheet.getRows();

        //4.一行对应的所有单元格
        Cell[] cells = null;

        File writeFile = new File("write.txt");
        if (!writeFile.exists())
            writeFile.createNewFile();
        FileWriter fileWriter = new FileWriter(writeFile);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

        for (int i = 0; i < rows; i++){
            cells = sheet.getRow(i);
            for (Cell cell:cells){
                bufferedWriter.write(cell.getContents());
                bufferedWriter.write(" ");
            }
            bufferedWriter.newLine();
        }

        bufferedWriter.flush();
        fileWriter.flush();
        bufferedWriter.close();
        fileWriter.close();
        workbook.close();
    }

    public static void main(String[] args) throws IOException, WriteException, BiffException {

        System.out.println("1.读取txt文件创建excel表:");
        File file = new File("test.txt");
        writeExcel(file);

        System.out.println("2.读取excel表格内容:");
        File excel = new File("test.xls");
        readExcel(excel);

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值