testbugs.java
来自「EXCEL read and write」· Java 代码 · 共 1,523 行 · 第 1/4 页
JAVA
1,523 行
/* ==================================================================== Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.==================================================================== */package org.apache.poi.hssf.usermodel;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.Iterator;import java.util.List;import junit.framework.AssertionFailedError;import junit.framework.TestCase;import org.apache.poi.hssf.HSSFTestDataSamples;import org.apache.poi.hssf.model.Workbook;import org.apache.poi.hssf.record.CellValueRecordInterface;import org.apache.poi.hssf.record.EmbeddedObjectRefSubRecord;import org.apache.poi.hssf.record.NameRecord;import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate;import org.apache.poi.hssf.record.formula.DeletedArea3DPtg;import org.apache.poi.hssf.record.formula.Ptg;import org.apache.poi.hssf.util.CellRangeAddress;import org.apache.poi.util.TempFile;/** * Testcases for bugs entered in bugzilla * the Test name contains the bugzilla bug id * @author Avik Sengupta * @author Yegor Kozlov */public final class TestBugs extends TestCase { private static HSSFWorkbook openSample(String sampleFileName) { return HSSFTestDataSamples.openSampleWorkbook(sampleFileName); } private static HSSFWorkbook writeOutAndReadBack(HSSFWorkbook original) { return HSSFTestDataSamples.writeOutAndReadBack(original); } private static void writeTestOutputFileForViewing(HSSFWorkbook wb, String simpleFileName) { if (true) { // set to false to output test files return; } File file; try { file = TempFile.createTempFile(simpleFileName + "#", ".xls"); FileOutputStream out = new FileOutputStream(file); wb.write(out); out.close(); } catch (IOException e) { throw new RuntimeException(e); } if (!file.exists()) { throw new RuntimeException("File was not written"); } System.out.println("Open file '" + file.getAbsolutePath() + "' in Excel"); } /** Test reading AND writing a complicated workbook *Test opening resulting sheet in excel*/ public void test15228() { HSSFWorkbook wb = openSample("15228.xls"); HSSFSheet s = wb.getSheetAt(0); HSSFRow r = s.createRow(0); HSSFCell c = r.createCell(0); c.setCellValue(10); writeTestOutputFileForViewing(wb, "test15228"); } public void test13796() { HSSFWorkbook wb = openSample("13796.xls"); HSSFSheet s = wb.getSheetAt(0); HSSFRow r = s.createRow(0); HSSFCell c = r.createCell(0); c.setCellValue(10); writeOutAndReadBack(wb); } /**Test writing a hyperlink * Open resulting sheet in Excel and check that A1 contains a hyperlink*/ public void test23094() { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet s = wb.createSheet(); HSSFRow r = s.createRow(0); r.createCell(0).setCellFormula("HYPERLINK( \"http://jakarta.apache.org\", \"Jakarta\" )"); writeTestOutputFileForViewing(wb, "test23094"); } /** test hyperlinks * open resulting file in excel, and check that there is a link to Google */ public void test15353() { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("My sheet"); HSSFRow row = sheet.createRow( 0 ); HSSFCell cell = row.createCell( 0 ); cell.setCellFormula("HYPERLINK(\"http://google.com\",\"Google\")"); writeOutAndReadBack(wb); } /** test reading of a formula with a name and a cell ref in one **/ public void test14460() { HSSFWorkbook wb = openSample("14460.xls"); wb.getSheetAt(0); } public void test14330() { HSSFWorkbook wb = openSample("14330-1.xls"); wb.getSheetAt(0); wb = openSample("14330-2.xls"); wb.getSheetAt(0); } private static void setCellText(HSSFCell cell, String text) { cell.setCellValue(new HSSFRichTextString(text)); } /** test rewriting a file with large number of unique strings *open resulting file in Excel to check results!*/ public void test15375() { HSSFWorkbook wb = openSample("15375.xls"); HSSFSheet sheet = wb.getSheetAt(0); HSSFRow row = sheet.getRow(5); HSSFCell cell = row.getCell(3); if (cell == null) cell = row.createCell(3); // Write test cell.setCellType(HSSFCell.CELL_TYPE_STRING); setCellText(cell, "a test"); // change existing numeric cell value HSSFRow oRow = sheet.getRow(14); HSSFCell oCell = oRow.getCell(4); oCell.setCellValue(75); oCell = oRow.getCell(5); setCellText(oCell, "0.3"); writeTestOutputFileForViewing(wb, "test15375"); } /** test writing a file with large number of unique strings *open resulting file in Excel to check results!*/ public void test15375_2() throws Exception{ HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(); String tmp1 = null; String tmp2 = null; String tmp3 = null; for (int i = 0; i < 6000; i++) { tmp1 = "Test1" + i; tmp2 = "Test2" + i; tmp3 = "Test3" + i; HSSFRow row = sheet.createRow(i); HSSFCell cell = row.createCell(0); setCellText(cell, tmp1); cell = row.createCell(1); setCellText(cell, tmp2); cell = row.createCell(2); setCellText(cell, tmp3); } writeTestOutputFileForViewing(wb, "test15375-2"); } /** another test for the number of unique strings issue *test opening the resulting file in Excel*/ public void test22568() { int r=2000;int c=3; HSSFWorkbook wb = new HSSFWorkbook() ; HSSFSheet sheet = wb.createSheet("ExcelTest") ; int col_cnt=0, rw_cnt=0 ; col_cnt = c; rw_cnt = r; HSSFRow rw ; rw = sheet.createRow(0) ; //Header row for(int j=0; j<col_cnt; j++){ HSSFCell cell = rw.createCell(j) ; setCellText(cell, "Col " + (j+1)) ; } for(int i=1; i<rw_cnt; i++){ rw = sheet.createRow(i) ; for(int j=0; j<col_cnt; j++){ HSSFCell cell = rw.createCell(j) ; setCellText(cell, "Row:" + (i+1) + ",Column:" + (j+1)) ; } } sheet.setDefaultColumnWidth(18) ; writeTestOutputFileForViewing(wb, "test22568"); } /**Double byte strings*/ public void test15556() { HSSFWorkbook wb = openSample("15556.xls"); HSSFSheet sheet = wb.getSheetAt(0); HSSFRow row = sheet.getRow(45); assertNotNull("Read row fine!" , row); } /**Double byte strings */ public void test22742() { openSample("22742.xls"); } /**Double byte strings */ public void test12561_1() { openSample("12561-1.xls"); } /** Double byte strings */ public void test12561_2() { openSample("12561-2.xls"); } /** Double byte strings File supplied by jubeson*/ public void test12843_1() { openSample("12843-1.xls"); } /** Double byte strings File supplied by Paul Chung*/ public void test12843_2() { openSample("12843-2.xls"); } /** Reference to Name*/ public void test13224() { openSample("13224.xls"); } /** Illegal argument exception - cannot store duplicate value in Map*/ public void test19599() { openSample("19599-1.xls"); openSample("19599-2.xls"); } public void test24215() { HSSFWorkbook wb = openSample("24215.xls"); for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets();sheetIndex++) { HSSFSheet sheet = wb.getSheetAt(sheetIndex); int rows = sheet.getLastRowNum(); for (int rowIndex = 0; rowIndex < rows; rowIndex++) { HSSFRow row = sheet.getRow(rowIndex); int cells = row.getLastCellNum(); for (int cellIndex = 0; cellIndex < cells; cellIndex++) { row.getCell(cellIndex); } } } } public void test18800() { HSSFWorkbook book = new HSSFWorkbook(); book.createSheet("TEST"); HSSFSheet sheet = book.cloneSheet(0); book.setSheetName(1,"CLONE"); sheet.createRow(0).createCell(0).setCellValue(new HSSFRichTextString("Test")); book = writeOutAndReadBack(book); sheet = book.getSheet("CLONE"); HSSFRow row = sheet.getRow(0); HSSFCell cell = row.getCell(0); assertEquals("Test", cell.getRichStringCellValue().getString()); } /** * Merged regions were being removed from the parent in cloned sheets */ public void test22720() { HSSFWorkbook workBook = new HSSFWorkbook(); workBook.createSheet("TEST"); HSSFSheet template = workBook.getSheetAt(0); template.addMergedRegion(new CellRangeAddress(0, 1, 0, 2)); template.addMergedRegion(new CellRangeAddress(1, 2, 0, 2)); HSSFSheet clone = workBook.cloneSheet(0); int originalMerged = template.getNumMergedRegions(); assertEquals("2 merged regions", 2, originalMerged);// remove merged regions from clone for (int i=template.getNumMergedRegions()-1; i>=0; i--) { clone.removeMergedRegion(i); } assertEquals("Original Sheet's Merged Regions were removed", originalMerged, template.getNumMergedRegions());// check if template's merged regions are OK if (template.getNumMergedRegions()>0) { // fetch the first merged region...EXCEPTION OCCURS HERE template.getMergedRegion(0); } //make sure we dont exception } /**Tests read and write of Unicode strings in formula results * bug and testcase submitted by Sompop Kumnoonsate * The file contains THAI unicode characters. */ public void testUnicodeStringFormulaRead() { HSSFWorkbook w = openSample("25695.xls"); HSSFCell a1 = w.getSheetAt(0).getRow(0).getCell(0); HSSFCell a2 = w.getSheetAt(0).getRow(0).getCell(1); HSSFCell b1 = w.getSheetAt(0).getRow(1).getCell(0); HSSFCell b2 = w.getSheetAt(0).getRow(1).getCell(1); HSSFCell c1 = w.getSheetAt(0).getRow(2).getCell(0); HSSFCell c2 = w.getSheetAt(0).getRow(2).getCell(1); HSSFCell d1 = w.getSheetAt(0).getRow(3).getCell(0); HSSFCell d2 = w.getSheetAt(0).getRow(3).getCell(1); if (false) { // THAI code page System.out.println("a1="+unicodeString(a1)); System.out.println("a2="+unicodeString(a2)); // US code page System.out.println("b1="+unicodeString(b1)); System.out.println("b2="+unicodeString(b2)); // THAI+US System.out.println("c1="+unicodeString(c1)); System.out.println("c2="+unicodeString(c2)); // US+THAI System.out.println("d1="+unicodeString(d1)); System.out.println("d2="+unicodeString(d2)); } confirmSameCellText(a1, a2); confirmSameCellText(b1, b2); confirmSameCellText(c1, c2); confirmSameCellText(d1, d2); HSSFWorkbook rw = writeOutAndReadBack(w); HSSFCell ra1 = rw.getSheetAt(0).getRow(0).getCell(0); HSSFCell ra2 = rw.getSheetAt(0).getRow(0).getCell(1); HSSFCell rb1 = rw.getSheetAt(0).getRow(1).getCell(0); HSSFCell rb2 = rw.getSheetAt(0).getRow(1).getCell(1); HSSFCell rc1 = rw.getSheetAt(0).getRow(2).getCell(0); HSSFCell rc2 = rw.getSheetAt(0).getRow(2).getCell(1); HSSFCell rd1 = rw.getSheetAt(0).getRow(3).getCell(0); HSSFCell rd2 = rw.getSheetAt(0).getRow(3).getCell(1); confirmSameCellText(a1, ra1); confirmSameCellText(b1, rb1); confirmSameCellText(c1, rc1); confirmSameCellText(d1, rd1);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?