2017-04-14 7 views
-1

ソースデータをXLS形式の表形式にする必要があるBIレポートがあります。C#Excelシートの内容を表形式に変換するスクリプト

ソースデータは、ツールを使用してシステムから毎日トリガーされます。 .xls形式ですが、単純な書式設定です。このデータをBIレポートに入力するときは、テーブルに変換する必要があります。

Excelファイルを毎日手動で更新するのではなく、プロセスを自動化する必要があります。クライアントツールからExcelファイルをダウンロードしてローカルシステムに保存するスクリプトがあります。

だから、1行で私は以下のでしょうメソッド必要があります。

入力:

BEFORE

所望の出力:

AFTER

注 - 私は書いていますがC#のWindowsアプリケーションのスクリプト。

答えて

0

以下は、使用できるサンプルJavaコードです。

import org.apache.poi.ss.util.AreaReference; 
    import org.apache.poi.ss.util.CellReference; 
    import org.apache.poi.xssf.usermodel.XSSFSheet; 
    import org.apache.poi.xssf.usermodel.XSSFTable; 
    import org.apache.poi.xssf.usermodel.XSSFWorkbook; 
    import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTable; 
    import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn; 
    import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumns; 
    import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableStyleInfo; 

    import java.io.File; 
    import java.io.FileInputStream; 
    import java.io.FileOutputStream; 
    import java.io.IOException; 

    public class Main{ 

     public static void main(String[] args) throws IOException { 
      FileInputStream input_document = new FileInputStream(new File("C:\\Users\\x228458\\Desktop\\ExcelExample.xlsx")); 
      XSSFWorkbook my_xlsx_workbook = new XSSFWorkbook(input_document); 
      XSSFSheet sheet = my_xlsx_workbook.getSheetAt(0); 
      int firstrowindex = sheet.getFirstRowNum(); 
      int lastrowindex = sheet.getLastRowNum(); 

      int firstcolumnindex = sheet.getRow(0).getFirstCellNum(); 
      int lastcolumnindex = sheet.getRow(0).getLastCellNum(); 
      int numberofcolumns=(lastcolumnindex-firstcolumnindex); 

      XSSFTable my_table = sheet.createTable(); 
      CTTable cttable = my_table.getCTTable(); 
      CTTableStyleInfo table_style = cttable.addNewTableStyleInfo(); 
      table_style.setName("TableStyleMedium9"); 
      table_style.setShowColumnStripes(false); 
      table_style.setShowRowStripes(true); 
      AreaReference my_data_range = new AreaReference(new CellReference(firstrowindex, firstcolumnindex), new CellReference(lastrowindex, lastcolumnindex-1)); 
      cttable.setRef(my_data_range.formatAsString()); 
      cttable.setDisplayName("MYTABLE"); 
      cttable.setName("Test"); 
      cttable.setId(2L); //you can use any integer as Id 

      CTTableColumns columns = cttable.addNewTableColumns(); 

      columns.setCount(numberofcolumns); 
      for (int i = 0; i < numberofcolumns; i++) 
      { 
       CTTableColumn column = columns.addNewTableColumn(); 
       column.setName("Column" + i); 
       column.setId(i+1); 
      } 
      FileOutputStream fileOut = new FileOutputStream("C:\\Users\\x228458\\Desktop\\Excel_Format_Table.xlsx"); 
      my_xlsx_workbook.write(fileOut); 
      fileOut.close(); 
     } 
    } 

注:ApacheのPOI 3.15 jarが

を使用しています
関連する問題