选择现有工作表

时间:2013-07-25 14:34:47

标签: apache-poi

如何选择我知道工作表名称的现有工作表?我想将代码附加到工作表但在不同的列中。在下面的代码中,我指的是wb.creatsheet(" Summary")行。

def sql_a = """

SELECT MOPACTIVITY.MOPID FROM MOPUSER.MOPACTIVITY WHERE UPPER(MOPACTIVITY.mopid) = 'JONATHAN MORNINGSTAR'
ORDER BY MOPACTIVITY.MOPID

    """

def date = new Date() 
def dts = date.format("yyyy-MM-dd-HH-mm-ss") 
File file = new File('X:/CCG/' + dts + ' CCG REPORT.xls')              
file.write("")

Workbook wb = new HSSFWorkbook()

try {
    Sheet sheet = wb.createSheet("Summary")
                sheet.setColumnWidth(0,8.43 * 256)
                int rowNum = 0

    Row row = sheet.createRow(rowNum)
    Cell cellMOPID = row.createCell(0)

                cellMOPID.setCellStyle(headerStyle)

                cellMOPID.setCellValue("")

                db.eachRow(sql_a) {

                                rowNum++
                                row = sheet.createRow(rowNum)
                                cellMOPID = row.createCell(0)

                                cellMOPID.setCellValue(it.MOPID)

                                file.append(it.MOPID + "\n")
                }   

} catch (Exception e) { System.out.println(e.toString()) }

1 个答案:

答案 0 :(得分:3)

如果您查看JavaDocs for the Workbook interface,您会看到createSheet(String)以及getSheet(String)方法。使用现有工作表名称调用它,您将获得现有工作表

您的代码将类似于:

Workbook wb = WorkbookFactory.create(new File("input.xls"));
Sheet sheet = wb.getSheet("Summary");
// Read and add to the existing sheet here!