使用Java中的JXL复制表

编程入门 行业动态 更新时间:2024-10-21 17:22:00
本文介绍了使用Java中的JXL复制表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想将现有XLS文档中的工作表复制到新位置。 如何使用JXL执行此操作?

I would like to copy a sheet from an existing XLS document to a new one to a new location. How could I do this with JXL?

Workbook w1 = Workbook.getWorkbook(new File("ExistingDocument.xls"), settings); WritableWorkbook w2 = Workbook.createWorkbook(new File("NewDocument.xls")); /* So here, I would like copy the first sheet from w1 to the second sheet of w2 ... */ w2.write(); w2.close(); w1.close();

编辑 w1.getSheet(0).getCell(0,0)不是 WritableCell ,所以我无法使用 copyTo 方法。 有没有办法将 w1 中的单元格/表格添加到 w2 工作簿? edit2: 所以我必须创建工作簿的可写副本到其他文件? ( edit3:或者还有其他任何可以执行此操作的免费库吗?)

edit: w1.getSheet(0).getCell(0, 0) is not a WritableCell, so I couldn't use the copyTo method. Is there any way to add a cell/sheet from w1 to w2 workbook? edit2: So do I have to create a writable copy of the workbook to an other file? (edit3: Or is there any other free lib which can do this?)

更新:

Update:

当我运行此代码时,我得到 jxlmon.AssertionFailed 行上的例外

When I run this code, I get jxlmon.AssertionFailed exceptions on line

WritableCellFormat newFormat = new WritableCellFormat(readFormat);

如果我删除此行并将代码更改为

If I remove this line and change the code to

newCell.setCellFormat(readFormat);

然后不复制单元格样式(字体,单元格边界等。)。

then the cell styles aren't copied (the fonts, the cell borders, etc.).

try { Workbook sourceDocument = Workbook.getWorkbook(new File("C:\\source.xls")); WritableWorkbook writableTempSource = Workbook.createWorkbook(new File("C:\\temp.xls"), sourceDocument); WritableWorkbook copyDocument = Workbook.createWorkbook(new File("C:\\copy.xls")); WritableSheet sourceSheet = writableTempSource.getSheet(0); WritableSheet targetSheet = copyDocument.createSheet("sheet 1", 0); for (int row = 0; row < sourceSheet.getRows(); row++) { for (int col = 0; col < sourceSheet.getColumns(); col++) { WritableCell readCell = sourceSheet.getWritableCell(col, row); WritableCell newCell = readCell.copyTo(col, row); CellFormat readFormat = readCell.getCellFormat(); /* exception on the following line */ WritableCellFormat newFormat = new WritableCellFormat(readFormat); newCell.setCellFormat(newFormat); targetSheet.addCell(newCell); } } copyDocument.write(); copyDocument.close(); writableTempSource.close(); sourceDocument.close(); } catch (Exception e) { e.printStackTrace(); }

我如何将单元格样式复制到新单元格?

How could I copy the cell styles too to the new cell?

推荐答案

如何将一个工作簿中的工作表复制到另一个工作簿中的新工作表?

这可以做到,但需要一点工作。首先,你必须复制它的单元格(在几个嵌套的for循环中)。对于每个单元格,您需要调用 copyTo()方法,该方法将生成深层副本。但是,格式只是浅层复制,因此您需要获取单元格格式并使用其复制构造函数,然后在刚刚复制的单元格上调用setCellFormat。然后将重复的单元格添加到新的电子表格中

This can be done, but requires a little work. Firstly, you have to copy it cell (within a couple of nested for loops). For each cell you need to invoke the copyTo() method, which will produce a deep copy. However the format is only shallow copied, so you will need to get the cell format and use the copy constructor of that, and then call setCellFormat on the cell you have just copied. Then add the duplicate cell to the new spreadsheet

代码可能如下所示:

for (int i = 0 ; i < numrows ; i++){ for (int j = 0 ; j < numcols ; j++){ readCell = sheet.getCell(i, j); newCell = readCell.copyTo(i, j); readFormat = readCell.getCellFormat(); newFormat = new WritableCellFormat(readFormat); newCell.setCellFormat(newFormat); newSheet.add(newCell); } }

资源:

  • JExcel API常见问题解答
  • JExcel API Frequently Asked Questions

更多推荐

使用Java中的JXL复制表

本文发布于:2023-10-16 16:34:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1498127.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Java   JXL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!