0

这是使用 Groovy 和 Apache POI 从 SQL 转到 Excel 的完整示例。这只是我为我的 AM 报告运行的 5 个之一。我将如何获取另一份这样的报告并将它们包含在一起以显示在具有多个工作表的一个 Excel 文件中?

我看到了 wb.createSheet("Sheet1") 行,但是如何将两个通道合并到一个工作簿中?

@Grab( 'org.apache.poi:poi:3.9' )
import groovy.sql.Sql
import static org.apache.poi.ss.usermodel.CellStyle.*
import static org.apache.poi.ss.usermodel.IndexedColors.*
import org.apache.poi.hssf.usermodel.*
import org.apache.poi.hssf.usermodel.HSSFWorkbook
import org.apache.poi.ss.usermodel.Cell
import org.apache.poi.ss.usermodel.CellStyle
import org.apache.poi.ss.usermodel.Row
import org.apache.poi.ss.usermodel.Sheet
import org.apache.poi.ss.usermodel.Workbook
import org.apache.poi.ss.usermodel.Font
import java.io.FileOutputStream

def db = Sql.newInstance(
    'jdbc:oracle:thin:@XX.XX.XX.XXX:XXXX:ORCL',
    'xxxxxxxx', 'xxxxxxxx', 'oracle.jdbc.xxxx.OracleDataSource')

def sql = """
SELECT MOPACTIVITY.MOPID ,
MOPACTIVITY.MOPSERVICEIMPACTED "Type",  
MOPACTIVITY.MOPSTATEACTIVITY "State", 
MOPACTIVITY.MOPSTATUS "Status",
TO_CHAR(MOPACTIVITY.MOPSTART, 'YYYY-MM-DD') "SOM", 
TO_CHAR(MOPACTIVITY.MOPSTART, 'HH24:MI') "STIME", 
TO_CHAR(MOPACTIVITY.MOPEND, 'YYYY-MM-DD') "EOM",
TO_CHAR(MOPACTIVITY.MOPEND, 'HH24:MI') "ETIME", 
MOPACTIVITY.MOPFLAGGED,
MOPACTIVITY.MOPAPPROVED,
MOPACTIVITY.MOPDESCRIPTION
FROM mopuser.mopactivity
WHERE MOPACTIVITY.mopstart between sysdate and (sysdate+14)
AND UPPER(MOPACTIVITY.MOPSTATUS) != 'COMPLETE'
AND UPPER(MOPACTIVITY.MOPSTATUS) != 'CANCELLED'
AND MOPACTIVITY.MOPINTRUSIVE != 'false'
AND MOPACTIVITY.MOPFLAGGED != 'true'
AND MOPACTIVITY.MOPAPPROVED != 'false'
AND 
(MOPACTIVITY.MOPSTATEIMPACT LIKE '%AL%'
OR MOPACTIVITY.MOPSTATEIMPACT LIKE '%AR%' 
OR MOPACTIVITY.MOPSTATEIMPACT LIKE '%CA%' 
OR MOPACTIVITY.MOPSTATEIMPACT LIKE '%DC%' 
)
ORDER BY MOPACTIVITY.MOPSTATEIMPACT
    """

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

try {
    Workbook wb = new HSSFWorkbook()
    Sheet sheet = wb.createSheet("Sheet1")
                sheet.setColumnWidth(0, 13 * 256)
                sheet.setColumnWidth(1, 41 * 256)
                sheet.setColumnWidth(2, 6 * 256)
                sheet.setColumnWidth(3, 12 * 256)
                sheet.setColumnWidth(4, 10 * 256)
                sheet.setColumnWidth(5, 9 * 256)
                sheet.setColumnWidth(6, 10 * 256)
                sheet.setColumnWidth(7, 9 * 256)
                sheet.setColumnWidth(8, 89 * 256)
                int rowNum = 0


    Row row = sheet.createRow(rowNum)
    Cell cellMOPID = row.createCell(0)
    Cell cellType = row.createCell(1)
    Cell cellStatus = row.createCell(2)
    Cell cellState = row.createCell(3)
    Cell cellStartDate = row.createCell(4)
    Cell cellStartTime = row.createCell(5)
    Cell cellEndDate = row.createCell(6)
    Cell cellEndTime = row.createCell(7)
    Cell cellDesc = row.createCell(8)

                Font headerFont = wb.createFont();
                headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD)
                CellStyle headerStyle = wb.createCellStyle()
                headerStyle.setFont(headerFont)

                cellMOPID.setCellStyle(headerStyle)
                cellType.setCellStyle(headerStyle)
                cellStatus.setCellStyle(headerStyle)
                cellState.setCellStyle(headerStyle)
                cellStartDate.setCellStyle(headerStyle)
                cellStartTime.setCellStyle(headerStyle)
                cellEndDate.setCellStyle(headerStyle)
                cellEndTime.setCellStyle(headerStyle)
                cellDesc.setCellStyle(headerStyle)

                cellMOPID.setCellValue("MOPID")
                cellType.setCellValue("Type")
                cellStatus.setCellValue("Status")
                cellState.setCellValue("State")
                cellStartDate.setCellValue("Start Date")
                cellStartTime.setCellValue("Start Time")
                cellEndDate.setCellValue("End Date")
                cellEndTime.setCellValue("End Time")
                cellDesc.setCellValue("Description")

                db.eachRow(sql) {
                                def desc = it.MOPDESCRIPTION
                                desc = desc.replaceAll("[\r\n]", "")

                                CellStyle wrapStyle = wb.createCellStyle()
                                wrapStyle.setWrapText(true)

                                rowNum++
                                row = sheet.createRow(rowNum)
                                cellMOPID = row.createCell(0)
                                cellType = row.createCell(1)
                                cellStatus = row.createCell(2)
                                cellState = row.createCell(3)
                                cellStartDate = row.createCell(4)
                                cellStartTime = row.createCell(5)
                                cellEndDate = row.createCell(6)
                                cellEndTime = row.createCell(7)
                                cellDesc = row.createCell(8)

                                cellDesc.setCellStyle(wrapStyle)

                                cellMOPID.setCellValue(it.MOPID)
                                cellType.setCellValue(it.TYPE)
                                cellStatus.setCellValue(it.STATUS)
                                cellState.setCellValue(it.STATE)
                                cellStartDate.setCellValue("" + it.SOM)
                                cellStartTime.setCellValue("" + it.STIME)
                                cellEndDate.setCellValue("" + it.EOM)
                                cellEndTime.setCellValue("" + it.ETIME)
                                cellDesc.setCellValue(desc)

                                file.append(it.MOPID + "\t" + it.TYPE + "\t" + it.STATUS + "\t" + it.STATE + "\t" + it.SOM + "\t" + it.STIME + "\t" + it.EOM + "\t" + it.ETIME + "\t" + desc + "\n")
                }   

    FileOutputStream fileOut = new FileOutputStream("X:\\OUTPUT/" + dts + " CWL.xls")
    wb.write(fileOut)
    fileOut.close()
} catch (Exception e) { System.out.println(e.toString()) }
4

0 回答 0