1
我正在使用jExcel API將數據寫入Excel工作表。以下是我的代碼工作正常。但如果我寫大量的數據到Excel表,我得到這個異常 -如何在Excel中添加其他工作表(如果第一張表已滿)
jxl.write.biff.RowsExceededException: The maximum number of rows permitted on
a worksheet been exceeded
以下是代碼。如果第一張紙不能再容納更多行,我怎樣才能將數據寫入另一張紙。有人可以幫我從這裏出去嗎?
public void write() throws IOException, WriteException {
readFileIPAddress();
File file = new File(inputFile);
WorkbookSettings wbSettings = new WorkbookSettings();
wbSettings.setLocale(new Locale("en", "EN"));
WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);
workbook.createSheet("Report", 0);
WritableSheet excelSheet = workbook.getSheet(0);
createLabel(excelSheet);
createContent(excelSheet);
workbook.write();
workbook.close();
}
private void createLabel(WritableSheet sheet) throws WriteException {
WritableFont times15pt = new WritableFont(WritableFont.TAHOMA, 11);
times = new WritableCellFormat(times15pt);
times.setWrap(true);
WritableFont times10ptBoldUnderline = new WritableFont(WritableFont.TAHOMA, 13, WritableFont.BOLD, false, UnderlineStyle.SINGLE);
timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline);
timesBoldUnderline.setWrap(true);
CellView cv = new CellView();
cv.setFormat(times);
cv.setFormat(timesBoldUnderline);
cv.setAutosize(true);
addCaption(sheet, 0, 0, "IP Address");
addCaption(sheet, 1, 0, "Digital Element Country Code");
addCaption(sheet, 2, 0, "Maxmind Country Code");
addCaption(sheet, 3, 0, "Neustar City");
addCaption(sheet, 4, 0, "Maxmind City");
addCaption(sheet, 5, 0, "Neustar Zip Code");
addCaption(sheet, 6, 0, "Maxmind Zip Code");
}
private void createContent(WritableSheet sheet) throws WriteException, RowsExceededException {
NaEdgeDb edge = null;
try {
while (!workQueue.isEmpty()) {
String ipAddress = workQueue.take();
edge = new NaEdgeDb();
edge.set_server_addr(serverIPAddress);
edge.set_id(10);
if (checkIPDuplicate.add(ipAddress)) {
resp = GeoLocationService.getLocationIp(ipAddress, 0);
edge.naQueryEdge(ipAddress);
addNumber(sheet, 0, j, ipAddress);
addNumber(sheet, 1, j, (edge.edge_twoLetterCountry));
addNumber(sheet, 2, j, (resp.getLocation() != null ? resp.getLocation().getCountry() : null));
addNumber(sheet, 3, j, (edge.edge_city));
addNumber(sheet, 4, j, (resp.getLocation() != null ? resp.getLocation().getCity() : null));
addNumber(sheet, 5, j, (edge.edge_postalCode));
addNumber(sheet, 6, j, (resp.getLocation() != null ? resp.getLocation().getZipCode() : null));
j++;
}
}
} catch (Exception e) {
getLogger().log(LogLevel.ERROR, e.fillInStackTrace());
e.printStackTrace();
}
}
private void addCaption(WritableSheet sheet, int column, int row, String s) throws RowsExceededException,
WriteException {
Label label;
label = new Label(column, row, s, timesBoldUnderline);
sheet.addCell(label);
}
private void addNumber(WritableSheet sheet, int column, int row, String str) throws WriteException,
RowsExceededException {
Label number;
number = new Label(column, row, str);
sheet.addCell(number);
}
更新的代碼: -
這仍然拋出同樣的異常。不知道爲什麼?我已經在此代碼中處理了新工作表的創建?這個代碼的任何問題?
private void createContent(WritableSheet sheet, WritableWorkbook workbook2) throws WriteException, RowsExceededException {
NaEdgeDb edge = null;
WritableSheet ws2 = workbook2.createSheet("sheet2", 2);
try {
while (!workQueue.isEmpty()) {
String ipAddress = workQueue.take(); // generateIPAddress();
edge = new NaEdgeDb();
edge.set_server_addr(serverIPAddress);
edge.set_id(10);
System.out.println(sheet.getRows());
if (checkIPDuplicate.add(ipAddress) && sheet.getRows() <= 65536) {
resp = GeoLocationService.getLocationIp(ipAddress, 0);
edge.naQueryEdge(ipAddress);
addNumber(sheet, 0, j, ipAddress);
addNumber(sheet, 1, j, (edge.edge_twoLetterCountry));
addNumber(sheet, 2, j, (resp.getLocation() != null ? resp.getLocation().getCountry() : null));
addNumber(sheet, 3, j, (edge.edge_city));
addNumber(sheet, 4, j, (resp.getLocation() != null ? resp.getLocation().getCity() : null));
addNumber(sheet, 5, j, (edge.edge_postalCode));
addNumber(sheet, 6, j, (resp.getLocation() != null ? resp.getLocation().getZipCode() : null));
j++;
} else {
resp = GeoLocationService.getLocationIp(ipAddress, 0);
edge.naQueryEdge(ipAddress);
addNumber(ws2, 0, j, ipAddress);
addNumber(ws2, 1, j, (edge.edge_twoLetterCountry));
addNumber(ws2, 2, j, (resp.getLocation() != null ? resp.getLocation().getCountry() : null));
addNumber(ws2, 3, j, (edge.edge_city));
addNumber(ws2, 4, j, (resp.getLocation() != null ? resp.getLocation().getCity() : null));
addNumber(ws2, 5, j, (edge.edge_postalCode));
addNumber(ws2, 6, j, (resp.getLocation() != null ? resp.getLocation().getZipCode() : null));
k++;
}
}
} catch (Exception e) {
getLogger().log(LogLevel.ERROR, e.fillInStackTrace());
e.printStackTrace();
}
System.out.println("Size of HashSet:" + checkIPDuplicate.size());
}
如何在代碼中使用相同的東西?你能在我的代碼中提出一些改變嗎? – ferhan
@TechGeeky:我已經更新了答案。請讓我知道,如果你需要更多的投入。我假設'createContent'是插入除標題以外的所有行的方法。 –
感謝Yogendra,我更新了代碼。你能看看我的代碼現在有什麼問題嗎? – ferhan