Domanda

I want to write a MySQL Data into Excel file. In that, I need to create table border for my data as well need to merging the few column headers.

I have done the mapping to Load the MySQL data into Excel Sheet. But I don't know the way to create table border as well merging the column headers.

Below code will help me to Merge the cells, But how to incorporate this code in between my two talend components,

tMySQLInput -> tFileOutputExcel


import java.io.*;

import org.apache.poi.ss.usermodel.*;

import org.apache.poi.hssf.usermodel.*;

import org.apache.poi.ss.util.CellRangeAddress;

public class 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();


    }

}

È stato utile?

Soluzione

you can use tJavaRow between your components. This component allows you to write java code and transform data. this component has input and output schema where you can specify input this component will take and output this component will give. section below for import will move to Advance Settings section of tJavaRow.

import java.io.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

You can write your java code in BAsic Settings section your refer to input data schema column by

input_row.column_name
and output as output_row.column_name

...(try to user GenerateCode option to see how input_row and output_row works)

Ideally for merging/transforming data you can also use tMap which can merge/transform rows and output the data to next level

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top