Jav*_*ser 8 java excel apache-poi
我得到了一个分配,我需要拆分电子表格的数据并将其写入新的电子表格.条件是,给定电子表格可能有多个合并单元格数,我需要找到那些合并单元格并在新的SpreadSheet中写入这些数据.即,必须在另一个电子表格中写入一个合并单元格之间的数据或单元格,直到另一个合并单元格.
我的努力准则如下,
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class CopyTest {
public static void main(String[] args) throws IOException {
CopyTest excel = new CopyTest();
excel.process("D:\\B3.xls");
}
public void process(String fileName) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fileName));
HSSFWorkbook workbook = new HSSFWorkbook(bis);
HSSFWorkbook myWorkBook = new HSSFWorkbook();
HSSFSheet sheet = null;
HSSFRow row = null;
HSSFCell cell = null;
HSSFSheet mySheet = null;
HSSFRow myRow = null;
HSSFCell myCell = null;
int sheets = workbook.getNumberOfSheets();
int fCell = 0;
int lCell = 0;
int fRow = 0;
int lRow = 0;
for (int iSheet = 0; iSheet < sheets; iSheet++) {
sheet = workbook.getSheetAt(iSheet);
if (sheet != null) {
mySheet = myWorkBook.createSheet(sheet.getSheetName());
fRow = sheet.getFirstRowNum();
System.out.println("First Row at"+fRow);
lRow = sheet.getLastRowNum();
for (int iRow = fRow; iRow <= lRow; iRow++) {
row = sheet.getRow(iRow);
myRow = mySheet.createRow(iRow);
if (row != null) {
fCell = row.getFirstCellNum();
lCell = row.getLastCellNum();
for (int iCell = fCell; iCell < lCell; iCell++) {
//if (mySheet.getMergedRegionAt(index)!=null)
System.out.println("Finding next merged Cells");
cell = row.getCell(iCell);
myCell = myRow.createCell(iCell);
if (cell != null) {
myCell.setCellType(cell.getCellType());
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_BLANK:
myCell.setCellValue("");
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
myCell.setCellValue(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_ERROR:
myCell.setCellErrorValue(cell.getErrorCellValue());
break;
case HSSFCell.CELL_TYPE_FORMULA:
myCell.setCellFormula(cell.getCellFormula());
break;
case HSSFCell.CELL_TYPE_NUMERIC:
myCell.setCellValue(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_STRING:
myCell.setCellValue(cell.getStringCellValue());
break;
default:
myCell.setCellFormula(cell.getCellFormula());
// System.out.println("Reading Cell value\t"+myCell);
}System.out.println("Reading Cell value\t"+myCell);
}
}
}
}
}
}
bis.close();
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream("D:\\Result Excel1.xls", true));
myWorkBook.write(bos);
bos.close();
}}
Run Code Online (Sandbox Code Playgroud)
使用此代码,我已实现将电子表格克隆到另一个新工作表中.在这里,我没有找到合并的Cell,getMergedCellRegionAt()帮助我并像A:4 D:12一样返回合并的单元格区域.我该如何处理.请帮助我,您的小小的努力表示赞赏.提前致谢.
rge*_*man 13
按照Javadoc文档HSSFSheet,getMergedCellRegionAt()在2008年被废弃,因为Region它的回报也不赞成,赞成的CellRangeAddress.它建议你应该使用getMergedRegion(int),它返回一个CellRangeAddress.
合并的区域数据不直接与单元格本身一起存储,而是与Sheet对象一起存储.因此,您不需要遍历行和单元格来查找它们是否是合并区域的一部分; 您只需要遍历工作表上的合并区域列表,然后将合并的区域添加到新工作表中addMergedRegion(CellRangeAddress).
for (int i = 0; i < sheet.getNumMergedRegions(); i++)
{
CellRangeAddress mergedRegion = sheet.getMergedRegion(i);
// Just add it to the sheet on the new workbook.
mySheet.addMergedRegion(mergedRegion);
}
Run Code Online (Sandbox Code Playgroud)
这些方法HSSFSheet是在Sheet界面,所以他们将与Apache的POI支持,.xls的(HSSF)或.xlsx(XSSF)任何Excel工作簿工作.
| 归档时间: |
|
| 查看次数: |
17839 次 |
| 最近记录: |