【发布时间】:2014-05-05 12:15:19
【问题描述】:
我想将 MySQL 数据写入 Excel 文件。在那,我需要为我的数据创建表格边框,还需要合并几个列标题。
我已经完成了将 MySQL 数据加载到 Excel 表中的映射。但我不知道如何创建表格边框以及合并列标题。
下面的代码将帮助我合并单元格,但是如何将此代码合并到我的两个 talend 组件之间,
tMySQLInput -> tFileOutputExcel
导入 java.io.*;
导入 org.apache.poi.ss.usermodel.*;
导入 org.apache.poi.hssf.usermodel.*;
导入 org.apache.poi.ss.util.CellRangeAddress;
公共类 MergeCellsAcrossColumns {
public static void main(String[] args) throws Exception{
/* Create Workbook and Worksheet */
HSSFWorkbook my_workbook = new HSSFWorkbook();
HSSFSheet my_sheet = my_workbook.createSheet("Merge Cells");
Row row = my_sheet.createRow((short) 1);
Cell cell = row.createCell((short) 1);
cell.setCellValue("Merge Data with Apache POI");
my_sheet.addMergedRegion(new CellRangeAddress(
1, // mention first row here
1, //mention last row here, it is 1 as we are doing a column wise merging
1, //mention first column of merging
5 //mention last column to include in merge
));
/* Write changes to the workbook */
FileOutputStream out = new FileOutputStream(new File("C:\\Merge_Across_Columns.xls"));
my_workbook.write(out);
out.close();
}
}
【问题讨论】:
-
在 talendforge 上有一个不错的 excel 输出,可以为您完成这项工作。 talendforge.org/exchange/…