【java】+对excel进行操作_1_

一、


 

重点:https://www.jb51.net/article/110805.htm

public void writeData() throws IOException, BiffException {
        //打开文件
        File file = new File("D:\\IDEA\\my\\测试文件\\工作簿1.xls");
        Workbook wb = Workbook.getWorkbook(file);
//        WritableWorkbook book = Workbook.createWorkbook(file);//无wb表示将数据写到一个新文件中
        WritableWorkbook book = Workbook.createWorkbook(file,wb);//有wb表示将数据写到原文件中,并不新建一个文件
        WritableSheet sheet = book.getSheet(1);
        try {
            sheet.addCell(new Label(6, 6, "zhanggggg"));
            book.write();
            book.close();
        } catch (WriteException e) {
            e.printStackTrace();
        }
    }

 


 

 

 

package com.zgx.excel;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.testng.annotations.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author 
 * @version 1.0
 * @time 2019/5/24 16:35
 */
public class Excel {
    /***
     * 需求:
     * 1、根据某个单元格的内容定位出此单元格的位置
     * 2、
     */
    InputStream inputStream;
    Workbook rwb;

    public void base() {
        /***
         * 读取表,并创建workbook对象
         * 备注:需要讲表格另存为xls格式才会读取成功,不能只改后缀名
         */
        {
            try {
                inputStream = new FileInputStream("表路径");
                try {
                    rwb = Workbook.getWorkbook(inputStream);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (BiffException e) {
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void ttt() {
        base();

        /***
         *对表进行操作
         */
        //获取第一张sheet表
        Sheet sheet1 = rwb.getSheet(0);

        //获取某个单元格的值
        Cell cell1 = sheet1.getCell(0, 0);
        String cell1string = cell1.getContents().toString();

        //根据内容查找单元格的位置
        Cell cell2 = sheet1.findCell("T_G2_05");
        String cell2string = cell2.getContents().toString();
        int row = cell2.getRow();
        int column = cell2.getColumn();
        System.out.println("T_G2_05:" + row + "行," + column + "列。   内容:" + cell2string);

        //验证
        System.out.println("值:" + cell1string);
    }
}

 

二、java操作excel常用的两种方式(poi、jxl)

https://blog.csdn.net/ryan007liu/article/details/89357447

pol方式:

maven依赖:

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>

代码:

    /**
     * 功能:拷贝sheet表
     *
     * @param excelFilePath   原文件路径
     * @param rootSheetName   原sheet表名称
     * @param newSheetNames   新sheet表名称
     * @param newData         新sheet表需要改变的数据(null 表示 复制后数据不变)
     * @param outxlsxFilePath 新文件路径
     */
    public static void copySheet(String excelFilePath, String rootSheetName, List<String> newSheetNames, List<String> newData, String outxlsxFilePath) {
        try {
            workbook = new XSSFWorkbook(new FileInputStream(excelFilePath));

            // 读取源sheet表中的数据
            rootSheet = workbook.getSheet(rootSheetName);
            int lastRowNum = rootSheet.getLastRowNum(); // 总行数
            int lastCellNum = rootSheet.getRow(0).getPhysicalNumberOfCells(); // 总列数

            // 创建新的sheet表 并把源sheet表中的数据复制进去
            XSSFSheet rootSheetPoint = workbook.getSheet(rootSheetName);
            int rootSheetIndex = workbook.getSheetIndex(rootSheetPoint);

            for (int i = 0; i < newSheetNames.size(); i++) {
                // 复制原sheet到新sheet 并修改新sheet表的名称
                XSSFSheet copySheet = workbook.cloneSheet(rootSheetIndex);
                String copySheetName = copySheet.getSheetName();
                int copySheetIndex = workbook.getSheetIndex(copySheetName);
                workbook.setSheetName(copySheetIndex, newSheetNames.get(i));

                if (newData != null) {
                    // 给第1行第0列的所有sheet表赋值(根据具体业务)
                    copySheet.getRow(1).getCell(0).setCellValue(newData.get(i));
                }
            }
            workbook.write(new FileOutputStream(outxlsxFilePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

 

posted @ 2019-05-24 16:16  淡怀  阅读(239)  评论(0编辑  收藏  举报