java excel xlsx表格操作之poi XSSFWorkbook插件,读写(指定)行列数据,根据关键字(集)标注目标单元格背景色

//import com.monitorjbl.xlsx.StreamingReader;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.format.CellFormatType;
import org.apache.poi.ss.formula.functions.Column;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.awt.Color;
import java.awt.Font;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;

import jxl.Workbook;
import jxl.format.Colour;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableCellFormat;

/**
 * 所需jar包(E:\relibs\poi-5.0.0):
 * 1.poi-5.0.0
 * 2.poi-ooxml-5.0.0
 * 3.poi-ooxml-lite-5.0.0
 * 4.lib\commons-collections4-4.4
 * 5.lib\commons-math3-3.6.1
 * 6.ooxml-lib\commons-compress-1.20
 * 7.ooxml-lib\xmlbeans-4.0.0
 * @其它说明
 * sheetLen=sheet.getLastRowNum,索引号[0,1,2,3..]
 * rowLen=row.getLastCellNum,为长度[1,2,3,4..]
 * */
public class XLSXUtil {

    public static ArrayList readRowsX(String filePath,int sheetIndex){
        ArrayList rows = null;
        try {
            XSSFWorkbook wb = new XSSFWorkbook(new BufferedInputStream(new FileInputStream(filePath)));
            Sheet sheet = wb.getSheetAt(sheetIndex);
            int sheetLen=sheet.getLastRowNum();
            rows = new ArrayList();
            for (int i=0;i data,int startRow,int sheetIndex){
        try {
             XSSFWorkbook wb = new XSSFWorkbook(new BufferedInputStream(new FileInputStream(f)));
//           XSSFSheet sheet = wb.createSheet(sheetName);//新建
             XSSFSheet sheet = wb.getSheetAt(sheetIndex);//修改
             for (int i = 0; i < data.size(); i++) {
                 XSSFRow row = sheet.createRow(i+startRow);
                 String[] dataRow=data.get(i);
                 for (int j = 0; j < dataRow.length; j++) {
                     row.createCell(j).setCellValue(dataRow[j]);
                 }
             }
            wb.write(new FileOutputStream(f));
            wb.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static String[] readRowX(String filePath,int rIndex,int sheetIndex){
        String[] rowArr  = null;
        try {
            XSSFWorkbook wb = new XSSFWorkbook(new BufferedInputStream(new FileInputStream(filePath)));
            //读取excel第一个工作簿
            Sheet sheet = wb.getSheetAt(sheetIndex);
            Row row=sheet.getRow(rIndex);
            int colLen=row.getLastCellNum();
            rowArr=new String[colLen];
            for (int j=0;j readColumns(String f,int[] indexs,int sheetIndex) {
        ArrayList list = null;
        try {
            XSSFWorkbook wb = new XSSFWorkbook(new BufferedInputStream(new FileInputStream(f)));
            Sheet sheet = wb.getSheetAt(sheetIndex);
            int rowLen=sheet.getLastRowNum();
            list = new ArrayList();
            for (int i=0;i

 

你可能感兴趣的:(java)