C# Script to Convert the content of Excel sheet to Tabular format -


i have bi report needs source data in tabular form in xls format.

the source data triggered system tool on daily basis. in .xls format, simple formatting. when feeding data bi report, must converted tables.

instead of manually updating excel file daily, want automate process. have script downloads excel file client tool , saves local system.

so in 1 line need method below:

input:

before

desired output:

after

note - writing script in c# windows application.

below sample java code can used.

    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 integer id              cttablecolumns columns = cttable.addnewtablecolumns();              columns.setcount(numberofcolumns);             (int = 0; < 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();         }     } 

note: apache poi 3.15 jars used


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -