I am using poi-2.5.1-final-20040804.jar with Microsoft Office 2004 for Mac. I have used POI to create a .xls file that is unreadable by Microsoft Excel. When I attempt to open the file created by POI, Excel reports a dialog, "Unable to read file." when I attempt to open it. The file is unreadable only when a hyperlink label is >= 128 characters. The exact same document opens fine if the hyperlink label is 127 characters or less. See also issue 32255. In this case, POI writes the XLS file correctly without an ArrayIndexOutOfBoundsException. The difference between the issues is that this hyperlink points to a different sheet. Here is a test program. Execute it with a 128 (or greater) character parameter to create a broken Excel file: import java.util.*; import java.io.*; import org.apache.poi.hssf.usermodel.*; /** * This class uses POI to create an XLS file which cannot be read by * Microsoft Excel. * * @author <a href="mailto:jesse@swank.ca">Jesse Wilson</a> */ public class ExcelHyperlink2 { public static void main(String[] args) throws IOException { if(args.length != 1) { System.out.println("Usage: ExcelHyperlink label"); return; } // the target sheet String targetSheetName = "2"; // create a workbook, sheet, row and cell HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet1 = workbook.createSheet("1"); HSSFRow row = sheet1.createRow((short)0); HSSFCell cell = row.createCell((short)0); // create a hyperlink in our cell, labelled by the argument String target = "\"#" + targetSheetName + "!B2\""; String label = "\"" + args[0] + "\""; String formula = "HYPERLINK(" + target + ", " + label + ")"; System.out.println(formula); cell.setCellFormula(formula); // create a target sheet HSSFSheet sheet2 = workbook.createSheet(targetSheetName); // write the file FileOutputStream xlsOut = new FileOutputStream(new File("hyperlink.xls")); workbook.write(xlsOut); xlsOut.close(); } }
I am using POI 2.5.1 final on Windows and experience the same issue. Running Java 1.4.2.
Using the latest code in SVN this test case works fine. Jason