Java-简单操作Excel

版权声明:本文为博主原创文章,转载请注明链接地址。谢谢! https://blog.csdn.net/wdy_2099/article/details/77413722

本文主要是简单介绍下poi和jxl工具针对excel的最基本的操作,旨在让大家对2个工具有一个最基本的了解,不适合大型应用。代码注释较为详尽,希望帮到大家。


相关Jar包

commons-io-2.2.jar
dom4j-1.6.1.jar
jxl.jar
poi-3.11-20141221.jar
poi-examples-3.11-20141221.jar
poi-excelant-3.11-20141221.jar
poi-ooxml-3.11-20141221.jar
poi-ooxml-schemas-3.11-20141221.jar
poi-scratchpad-3.11-20141221.jar
xmlbeans-2.6.0.jar

jxl生成Excel

package com.wdy.excel;

import java.io.File;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class JxlExpExcel {

    /**
     * JXL创建Excel文件
     * @author wangdy
     */
    public static void main(String[] args) {
        //标题行内容
        String[] title = {"id","name","sex"};
        //创建Excel文件
        File file = new File("e:/jxl_test.xls");
        try {
            file.createNewFile();
            //创建工作簿
            WritableWorkbook workbook = Workbook.createWorkbook(file);
            //创建sheet
            WritableSheet sheet = workbook.createSheet("sheet1", 0);
            Label label = null;
            //第一行设置列名
            for (int i = 0; i < title.length; i++) {
                label = new Label(i,0,title[i]);
                sheet.addCell(label);   
            }
            //追加数据
            for (int i = 1; i < 10; i++) {
                label = new Label(0,i,"a" + 1);
                sheet.addCell(label);
                label = new Label(1,i,"user" + i);
                sheet.addCell(label);
                label = new Label(2,i,"男");
                sheet.addCell(label);
            }
            //写入数据
            workbook.write();
            workbook.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

jxl解析Excel

package com.wdy.excel;

import java.io.File;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

public class JxlReadExcel {
    /**
     * JXL解析Excel
     * @author wangdy
     */
    public static void main(String[] args) {

        try {
            //创建workbook
            Workbook workbook = Workbook.getWorkbook(new File("e:/jxl_test.xls"));
            //获取第一个工作表sheet
            Sheet sheet = workbook.getSheet(0);
            //获取数据
            for (int i = 0; i < sheet.getRows(); i++) {
                for (int j = 0; j < sheet.getColumns(); j++) {
                    Cell cell = sheet.getCell(j,i);
                    System.out.print(cell.getContents() + "  ");
                }
                System.out.println();
            }
            workbook.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

poi生成Excel(.xls)

package com.wdy.excel;

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

import org.apache.commons.io.FileUtils;
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;

public class PoiExpExcel {

    /**
     * POI生成Excel文件
     * @author wangdy
     */
    public static void main(String[] args) {
        //标题行内容
        String[] title = {"id","name","sex"};

        //创建Excel工作簿
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建一个工作表sheet
        HSSFSheet sheet = workbook.createSheet();
        //创建第一行
        HSSFRow row = sheet.createRow(0);
        HSSFCell cell = null;
        //插入第一行数据 id,name,sex
        for (int i = 0; i < title.length; i++) {
            cell = row.createCell(i);
            cell.setCellValue(title[i]);
        }
        //追加数据
        for (int i = 1; i <= 10; i++) {
            HSSFRow nextrow = sheet.createRow(i);
            HSSFCell cell2 = nextrow.createCell(0);
            cell2.setCellValue("a" + i);
            cell2 = nextrow.createCell(1);
            cell2.setCellValue("user" + i);
            cell2 = nextrow.createCell(2);
            cell2.setCellValue("男");
        }
        //创建一个文件
        File file = new File("e:/poi_test.xls");
        try {
            file.createNewFile();
            //将Excel内容存盘
            FileOutputStream stream = FileUtils.openOutputStream(file);
            workbook.write(stream);
            stream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

poi生成Excel(.xlsx)

package com.wdy.excel;

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

import org.apache.commons.io.FileUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class PoiExpExcelXlsx {

    /**
     * POI生成Excel文件
     * @author wangdy
     * @param args
     */
    public static void main(String[] args) {
        //标题行内容
        String[] title = {"id","name","sex"};

        //创建Excel工作簿
        XSSFWorkbook workbook = new XSSFWorkbook();
        //创建一个工作表sheet
        Sheet sheet = workbook.createSheet();
        //创建第一行
        Row row = sheet.createRow(0);
        Cell cell = null;
        //插入第一行数据 id,name,sex
        for (int i = 0; i < title.length; i++) {
            cell = row.createCell(i);
            cell.setCellValue(title[i]);
        }
        //追加数据
        for (int i = 1; i <= 10; i++) {
            Row nextrow = sheet.createRow(i);
            Cell cell2 = nextrow.createCell(0);
            cell2.setCellValue("a" + i);
            cell2 = nextrow.createCell(1);
            cell2.setCellValue("user" + i);
            cell2 = nextrow.createCell(2);
            cell2.setCellValue("男");
        }
        //创建一个文件
        File file = new File("e:/poi_xlsx_test.xlsx");
        try {
            file.createNewFile();
            //将Excel内容存盘
            FileOutputStream stream = FileUtils.openOutputStream(file);
            workbook.write(stream);
            stream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

poi解析Excel

package com.wdy.excel;

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

import org.apache.commons.io.FileUtils;
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;

public class PoiReadExcel {

    /**
     * POI解析Excel文件内容
     * @author wangdy
     */
    public static void main(String[] args) {

        //需要解析的Excel文件
        File file = new File("e:/poi_test.xls");
        try {
            //创建Excel,读取文件内容
            HSSFWorkbook workbook = 
                new HSSFWorkbook(FileUtils.openInputStream(file));
            //获取第一个工作表workbook.getSheet("Sheet0");
//          HSSFSheet sheet = workbook.getSheet("Sheet0");
            //读取默认第一个工作表sheet
            HSSFSheet sheet = workbook.getSheetAt(0);
            int firstRowNum = 0;
            //获取sheet中最后一行行号
            int lastRowNum = sheet.getLastRowNum();
            for (int i = firstRowNum; i <=lastRowNum; i++) {
                HSSFRow row = sheet.getRow(i);
                //获取当前行最后单元格列号
                int lastCellNum = row.getLastCellNum();
                for (int j = 0; j < lastCellNum; j++) {
                    HSSFCell cell = row.getCell(j);
                    String value = cell.getStringCellValue();
                    System.out.print(value + "  ");
                }
                System.out.println();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/wdy_2099/article/details/77413722