Java 如果第一张工作表已满,如何在Excel中添加其他工作表
我正在使用jexcelapi将数据写入excel工作表。下面是我的代码,运行良好。但是,如果我正在将大量数据写入Excel工作表,我会遇到这个异常-Java 如果第一张工作表已满,如何在Excel中添加其他工作表,java,jexcelapi,Java,Jexcelapi,我正在使用jexcelapi将数据写入excel工作表。下面是我的代码,运行良好。但是,如果我正在将大量数据写入Excel工作表,我会遇到这个异常- jxl.write.biff.RowsExceededException: The maximum number of rows permitted on a worksheet been exceeded 下面是代码。如果第一张图纸不能容纳更多行,如何将数据写入另一张图纸。有人能帮我吗 public void write() throws I
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());
}
private void createContent(可写工作表,可写工作簿工作簿2)引发WriteException,RowsExcepedException{
NaEdgeDb edge=null;
WritableSheet ws2=工作簿2.createSheet(“sheet2”,2);
试一试{
而(!workQueue.isEmpty()){
字符串ipAddress=workQueue.take();//generateIPAddress();
edge=新的NaEdgeDb();
edge.set\u server\u addr(serverIPAddress);
边缘。设置_id(10);
System.out.println(sheet.getRows());
如果(选中IPDuplicate.add(ipAddress)和&sheet.getRows()我认为您可以使用)在需要时创建新工作表
在createContent
方法中传递WritableWorkbook
而不是WritableSheet
。使用一些计数器检查当前工作表中添加的行数。如果工作表已满(计数器==最大行数),创建一个新的工作表并将行计数器重置为0。我如何在代码中使用相同的东西?你能建议对我的代码进行一些更改吗?@TechGeeky:我已经在更新答案。如果需要更多输入,请告诉我。我假设createContent
是插入除标题以外的所有行的方法。谢谢Yogendra,我更新了代码。您能看看我的代码现在有什么问题吗?@TechGeeky:您使用了或(| |)
条件,如果第一个条件为真,则不会对其进行计算。这种行为称为短路。因此,如果选中IPDuplicate.add(ipAddress)
为true,则不会计算行检查。将条件更改为和(&&&&)
。当行达到65536时,您需要创建一个新工作表,并继续在新工作表中添加内容。请更改这两项。它应该可以正常工作。再次更新代码。我修复了和(&&&&)
条件,关于另一张工作表,我已经在else循环中使用了一张新工作表ws
。这样不行吗?