我正在用Java编写一个应用程序,它使用JexcelAPI创建一个Excel 2003文件,将数据写入其中(17列,15,000行)。从可靠的数据流接收数据。到目前为止一切都很好 - 它完美无缺。 当我试图在某些单元格中放置超文本时(实际上在一列中),问题就出现了。从这一刻起,文件保存失败。 代码:
public class partsData
{
private final static String PdbFileName = "partsDB.xls";
private static WorkbookSettings PdbWorkSheet = null;
private static WritableWorkbook PdbWorkbook = null;
private static WritableSheet PdbWritableSheet = null;
WritableHyperlink wh = null;
public static void main (String args[])
{
Int linesCounter = 2;
try
{
PdbWorkSheet = new WorkbookSettings();
PdbWorkbook = Workbook.createWorkbook(new File(PdbFileName), PdbWorkSheet);
PdbWritableSheet = PdbWorkbook.createSheet(PdbFileName, 0);
}
catch (IOException e) { filesClose("File open failed - program aborts\n"); }
try
{ // Write to the cells
lr = new Label(10, linesCounter, splitLine[0]);
PdbWritableSheet.addCell(lr);
}
catch (WriteException e) { logFileWrite("Write failure - #" + linesCounter); }
// Create hyperlink fields based on the data already embedded in the Excel
String sStr = <File name and path>
String tStr = <field text>
if ((sStr != "") && (tStr != "") && (linesCounter > 1))
{
file = new File (sStr);
wh = new WritableHyperlink(4, linesCounter, file);
try { PdbWritableSheet.addHyperlink(wh); }
catch (RowsExceededException e) { logFileWrite("REE); }
catch (WriteException e) { logFileWrite("WE); }
}
else
logFileWrite("No hyperlink");
}
try
{
PdbWorkbook.write(); // This is where the exception occurs
PdbWorkbook.close();
}
catch (IOException | WriteException e) { logFileWrite("close failure); };
logFileWrite(message);
}
}
错误消息:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(Unknown Source)
at jxl.write.biff.HyperlinkRecord.getFileData(HyperlinkRecord.java:1011)
at jxl.write.biff.HyperlinkRecord.getData(HyperlinkRecord.java:518)
at jxl.biff.WritableRecordData.getBytes(WritableRecordData.java:71)
at jxl.write.biff.File.write(File.java:147)
at jxl.write.biff.SheetWriter.write(SheetWriter.java:570)
任何帮助将不胜感激 问候 Ganish