VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > Java教程 >
  • Java 操作 Excel(1)--Jxl使用

Jxl 是一个操作 Excel 的工具类库,功能比较简单,只支持 Excel 2003 及以前的版本;本文简单介绍下使用 Jxl 来读写 Excel,文中所使用到的软件版本:jdk1.8.0_181、Jxl 2.6.12。

1、引入依赖

<dependency>
    <groupId>net.sourceforge.jexcelapi</groupId>
    <artifactId>jxl</artifactId>
    <version>2.6.12</version>
</dependency>

2、代码样例

复制代码
package com.abc.demo.general.excel;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.*;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 使用Jxl操作Excel
 */
public class JxlCase {
    private static Logger logger = LoggerFactory.getLogger(JxlCase.class);

    @Test
    public void write() throws Exception {
        WritableWorkbook writableWorkbook = null;
        try {
            writableWorkbook = Workbook.createWorkbook(new File("d:/a.xls"));
            WritableSheet writableSheet = writableWorkbook.createSheet("数据", 0);
            //写入5行数据
            for (int row = 0; row < 5; row++) {
                int col = 0;
                //写入数字
                writableSheet.addCell(new jxl.write.Number(col++, row, row));
                //写入文本
                writableSheet.addCell(new Label(col++, row, "文本"));
                //写入布尔值
                writableSheet.addCell(new jxl.write.Boolean(col++, row, true));
                //设置格式
                WritableFont writableFont = new WritableFont(WritableFont.TIMES,16, WritableFont.BOLD);
                WritableCellFormat writableCellFormat = new WritableCellFormat(writableFont);
                writableCellFormat.setAlignment(jxl.format.Alignment.CENTRE);
                writableCellFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
                writableCellFormat.setWrap(true);
                writableSheet.addCell(new Label(col++, row, "格式化", writableCellFormat));
            }
            writableWorkbook.write();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (writableWorkbook != null) {
                writableWorkbook.close();
            }
        }
    }

    @Test
    public void read() {
        Workbook workbook = null;
        try {
            workbook = Workbook.getWorkbook(new File("d:/a.xls"));
            Sheet sheet = workbook.getSheet(0);
            logger.info("colNum={},rowNum={}", sheet.getColumns(), sheet.getRows());
            List<String> lineData = new ArrayList<>();
            for (int i = 0; i < sheet.getRows(); i++) {
                lineData.clear();
                for (int j = 0; j < sheet.getColumns(); j++) {
                    Cell cell = sheet.getCell(j, i);
                    lineData.add(cell.getContents());
                }
                logger.info("第{}行数据:{}", i + 1, lineData.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (workbook != null) {
                workbook.close();
            }
        }
    }

}
复制代码

 来源:https://www.cnblogs.com/wuyongyin/p/14691139.html


相关教程