当前位置: 首页 > 知识库问答 >
问题:

如何使用SXSSF修改单元格颜色编辑大数据量的excel文件

法风畔
2023-03-14

如何用SXSSF流式api编辑已有的大型excel文件

如何使用SXSSF写入现有文件?

我有一个现有的文件,我需要更新内容,更改一些内容的单元格颜色。

    null

如何通过更改单元格颜色来更新结果文件?

    package pageobjects;

    import java.awt.Color;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.nio.file.FileSystem;
    import java.nio.file.FileSystems;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.StandardCopyOption;

    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFCellStyle;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.sl.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.CellStyle;
    import org.apache.poi.ss.usermodel.DataFormatter;
    import org.apache.poi.ss.usermodel.FillPatternType;
    import org.apache.poi.ss.usermodel.IndexedColors;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.util.CellReference;
    import org.apache.poi.xssf.streaming.SXSSFCell;
    import org.apache.poi.xssf.streaming.SXSSFRow;
    import org.apache.poi.xssf.streaming.SXSSFSheet;
    import org.apache.poi.xssf.streaming.SXSSFWorkbook;
    import org.apache.poi.xssf.usermodel.XSSFCell;
    import org.apache.poi.xssf.usermodel.XSSFCellStyle;
    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 org.testng.Reporter;

    import property.IHomePage;
    import utility.SeleniumUtils;

    public class Excelcom2try extends SeleniumUtils implements IHomePage {
            public static FileOutputStream opstr = null;
            XSSFCellStyle cellStyleRed = null;
            SXSSFWorkbook sxssfWorkbook = null;
            SXSSFSheet sheet = null;
            SXSSFRow row3edit = null;
            SXSSFCell Cell = null;
            @SuppressWarnings("resource")
    public void compare() {
            try {
                    // Create new file for Result
                    XSSFWorkbook workbook = new XSSFWorkbook();
                    FileOutputStream fos = new FileOutputStream(new File("\\\\sd\\comparisonfile\\ResultFile.xlsx"));
                    workbook.write(fos);
                    workbook.close();
                    Thread.sleep(2000);
                    // get input for 2 compare excel files
                    FileInputStream excellFile1 = new FileInputStream(new File("new File("\\\\sd\\comparisonfile\\UAT_Relationship.xlsx"));
                    FileInputStream excellFile2 = new FileInputStream(new File(""\\\\sd\\comparisonfile\\Prod_Relationship.xlsx"));
                    // Copy file 2 for result to highlight not equal cell
                    FileSystem system = FileSystems.getDefault();
                    Path original = system.getPath(""\\\\sd\\comparisonfile\\Prod_Relationship.xlsx");
                    Path target = system.getPath(""\\\\sd\\comparisonfile\\ResultFile.xlsx");

                    try {
                            // Throws an exception if the original file is not found.
                            Files.copy(original, target, StandardCopyOption.REPLACE_EXISTING);
                            Reporter.log("Successfully Copy File 2 for result to highlight not equal cell");
                            Add_Log.info("Successfully Copy File 2 for result to highlight not equal cell");
                    } catch (IOException ex) {
                            Reporter.log("Unable to Copy File 2 ");
                            Add_Log.info("Unable to Copy File 2 ");
                    }
                    Thread.sleep(2000);
                    FileInputStream excelledit3 = new FileInputStream(new File("\\\\sd\\comparisonfile\\ResultFile.xlsx"));
                    // Create Workbook for 2 compare excel files
                    XSSFWorkbook workbook1 = new XSSFWorkbook(excellFile1);
                    XSSFWorkbook workbook2 = new XSSFWorkbook(excellFile2);
                    // Temp workbook
                    XSSFWorkbook workbook3new = new XSSFWorkbook();
                    //XSSF cellStyleRed as  SXSSFWorkbook cannot have cellstyle  color
                    cellStyleRed = workbook3new.createCellStyle();
                    cellStyleRed.setFillForegroundColor(IndexedColors.RED.getIndex());
                    cellStyleRed.setFillPattern(FillPatternType.SOLID_FOREGROUND);

                    // Get first/desired sheet from the workbook to compare both excel sheets
                    XSSFSheet sheet1 = workbook1.getSheetAt(0);
                    XSSFSheet sheet2 = workbook2.getSheetAt(0);
                    //XSSFWorkbook workbook3new temp convert to SXSSFWorkbook
                    // keep 100 rows in memory, exceeding rows will be flushed to disk
                    sxssfWorkbook = new SXSSFWorkbook(100);
                    sxssfWorkbook.setCompressTempFiles(true);
                    sheet = sxssfWorkbook.createSheet();
                    // Compare sheets
                    if (compareTwoSheets(sheet1, sheet2, sheet)) {

                            Reporter.log("\\n\\nThe two excel sheets are Equal");
                            Add_Log.info("\\n\\nThe two excel sheets are Equal");
                    } else {
                            Reporter.log("\\n\\nThe two excel sheets are Not Equal");
                            Add_Log.info("\\n\\nThe two excel sheets are Not Equal");

                    }

                    // close files
                    excellFile1.close();
                    excellFile2.close();
            // excelledit3.close();

                    opstr.close();
                     // dispose of temporary files backing this workbook on disk

            }catch (Exception e) {
                    e.printStackTrace();
            }
            Reporter.log("Successfully Close All files");
            Add_Log.info("Successfully Close All files");
    }

    // Compare Two Sheets
    public boolean compareTwoSheets(XSSFSheet sheet1, XSSFSheet sheet2, SXSSFSheet sheet) throws IOException {
            int firstRow1 = sheet1.getFirstRowNum();
            int lastRow1 = sheet1.getLastRowNum();
            boolean equalSheets = true;
            for (int i = firstRow1; i <= lastRow1; i++) {

                    Reporter.log("\n\nComparing Row " + i);
                    Add_Log.info("\n\nComparing Row " + i);
                    XSSFRow row1 = sheet1.getRow(i);
                    XSSFRow row2 = sheet2.getRow(i);
                    //row3edit = sheet.getRow(i);
                    for(int rownum = 0; rownum < 100; rownum++){
                            row3edit= sheet.createRow(rownum);
                    }
                    if (!compareTwoRows(row1, row2, row3edit)) {
                            equalSheets = false;
                            // Write if not equal
    // Get error here java.lang.NullPointerException for row3edit.setRowStyle(cellStyleRed);
                            //if disable test is completed Successfully without writing result file
                            row3edit.setRowStyle(cellStyleRed);
                            Reporter.log("Row " + i + " - Not Equal");
                            Add_Log.info("Row " + i + " - Not Equal");
                            // break;
                    } else {
                            Reporter.log("Row " + i + " - Equal");
                            Add_Log.info("Row " + i + " - Equal");
                    }
            }

            // Write if not equal
            opstr = new FileOutputStream(""\\\\sd\\comparisonfile\\ResultFile.xlsx");
            sxssfWorkbook.write(opstr);

            opstr.close();

            return equalSheets;
    }

    // Compare Two Rows
    public boolean compareTwoRows(XSSFRow row1, XSSFRow row2, SXSSFRow row3edit) throws IOException {
            if ((row1 == null) && (row2 == null)) {
                    return true;
            } else if ((row1 == null) || (row2 == null)) {
                    return false;
            }

            int firstCell1 = row1.getFirstCellNum();
            int lastCell1 = row1.getLastCellNum();
            boolean equalRows = true;

            // Compare all cells in a row

            for (int i = firstCell1; i <= lastCell1; i++) {
                    XSSFCell cell1 = row1.getCell(i);
                    XSSFCell cell2 = row2.getCell(i);
                     for(int cellnum = 0; cellnum < 10; cellnum++){
                             Cell = row3edit.createCell(cellnum);
                   String address = new CellReference(Cell).formatAsString();
                   Cell.setCellValue(address);
               }
                    if (!compareTwoCells(cell1, cell2)) {
                            equalRows = false;
                            Reporter.log("       Cell " + i + " - NOt Equal " + cell1 + "  ===  " + cell2);
                            Add_Log.info("       Cell " + i + " - NOt Equal " + cell1 + "  ===  " + cell2);
                            break;
                    } else {
                            Reporter.log("       Cell " + i + " - Equal " + cell1 + "  ===  " + cell2);
                            Add_Log.info("       Cell " + i + " - Equal " + cell1 + "  ===  " + cell2);
                    }
            }
            return equalRows;
    }

    // Compare Two Cells
    @SuppressWarnings("deprecation")
    public static boolean compareTwoCells(XSSFCell cell1, XSSFCell cell2) {
            if ((cell1 == null) && (cell2 == null)) {
                    return true;
            } else if ((cell1 == null) || (cell2 == null)) {
                    return false;
            }

            boolean equalCells = false;
            int type1 = cell1.getCellType();
            int type2 = cell2.getCellType();
            if (type2 == type1) {
                    if (cell1.getCellStyle().equals(cell2.getCellStyle())) {
                            // Compare cells based on its type
                            switch (cell1.getCellType()) {
                            case HSSFCell.CELL_TYPE_FORMULA:
                                    if (cell1.getCellFormula().equals(cell2.getCellFormula())) {
                                            equalCells = true;
                                    } else {
                                    }
                                    break;

                            case HSSFCell.CELL_TYPE_NUMERIC:
                                    if (cell1.getNumericCellValue() == cell2.getNumericCellValue()) {
                                            equalCells = true;
                                    } else {
                                    }
                                    break;
                            case HSSFCell.CELL_TYPE_STRING:
                                    if (cell1.getStringCellValue().equals(cell2.getStringCellValue())) {
                                            equalCells = true;
                                    } else {
                                    }
                                    break;
                            case HSSFCell.CELL_TYPE_BLANK:
                                    if (cell2.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
                                            equalCells = true;

                                    } else {
                                    }
                                    break;
                            case HSSFCell.CELL_TYPE_BOOLEAN:
                                    if (cell1.getBooleanCellValue() == cell2.getBooleanCellValue()) {
                                            equalCells = true;
                                    } else {
                                    }
                                    break;
                            case HSSFCell.CELL_TYPE_ERROR:
                                    if (cell1.getErrorCellValue() == cell2.getErrorCellValue()) {
                                            equalCells = true;
                                    } else {
                                    }
                                    break;
                            default:
                                    if (cell1.getStringCellValue().equals(cell2.getStringCellValue())) {
                                            equalCells = true;
                                    } else {
                                    }
                                    break;
                            }
                    } else {
                            return false;
                    }
            } else {
                    return false;
            }
            return equalCells;
    }

}

共有1个答案

糜淇
2023-03-14

一般来说,40,000行是一个非常小的数据量,我不想建议在这种情况下使用流。相反,只要提供足够的内存,就可以轻松地在XSSF工作簿中保存40000行。使用XSSF工作簿,您可以直接修改您尝试过的内容。

但是,如果你工作的数据真的很大,只有1mill。行和许多列,则以下方法将有所帮助:

1)安装Excel流式阅读器2)同时读取文件F1和F2并逐行和逐单元格进行比较3)根据发现的差异,创建一个新行和新单元格,并将其写入结果文件F3

 类似资料:
  • 我有一系列基于用户选择的EditText单元格,其中一些单元格将自动填充,然后我通过执行“myCell[x].setEnabled(false)”将这些EditText单元格更改为不可编辑。在这种情况下,字体颜色变为灰色,可读性不好。有没有一种方法可以改变EditText字体的颜色。setEnabled(false)或是否有其他方法通过对字体类型和颜色的更多控制来禁用单元格的可编辑性?我可以改变背

  • 我有一个很大的.xlsx excel工作表,有40万行。我想在现有的工作簿中阅读和写作。 当我尝试使用Apache poi在java中阅读它时,使用了以下代码: 这段代码的第二行占用最多5GB的RAM。 是否有任何方法可以对现有的大型excel工作簿进行读写操作,这些工作簿的行数超过40万行。

  • 问题内容: 我使用xlwt Python库在excel工作簿中写入数据。 现在我在将背景色添加到excel单元时遇到了一些问题。 例如,我在RGB(10,20,30)中有下一个颜色,最简单的方法是什么?有什么办法可以将此颜色设置为单元格吗? 问题答案: 在此示例中,我展示了如何设置单元格的背景色,您可以将其运行以得到结果:

  • 我正在使用Apache POI读取零件编号电子表格中的数据。我在我们的数据库中查找零件编号,如果我们有零件的计算机辅助设计图纸,我将零件编号单元格涂成绿色,如果没有,我将其涂成红色。处理完成后,将保存电子表格。我遇到的问题是那列中的每个细胞都是绿色的。我已经完成了代码,查找零件号的逻辑工作正常,确定单元格应该是什么颜色以及设置颜色和填充的逻辑似乎也工作正常。知道我做错了什么吗? 谢谢

  • 我正在使用POI()的流式工作表编写一个大型Excel表(超过一百万条记录),但我得到的是。下面是我的代码:

  • 我需要用Java和Jsoup解析一个表,并根据它的值改变单元格的颜色。这就是html表格的外观,也是单元格颜色需要定义的方式 我写了一个脚本,可以准备好单元格的值并从中更改文本,但是我无法也更改颜色。 这是我收到的错误: