【问题标题】:C# Script to Convert the content of Excel sheet to Tabular format将 Excel 工作表的内容转换为表格格式的 C# 脚本
【发布时间】:2017-04-14 12:49:54
【问题描述】:

我有一个 BI 报告,它需要源数据采用 XLS 格式的表格形式。

每天都会从一个工具向我的系统触发源数据。它采用 .xls 格式,但格式简单。当将此数据输入我的 BI 报告时,必须将其转换为表格。

我不想每天手动更新 Excel 文件,而是想自动化该过程。我有从客户端工具下载 Excel 文件并将其保存到本地系统的脚本。

所以在一行中,我需要一种方法来执行以下操作:

输入:

期望的输出:

注意 - 我在 C# windows 应用程序中编写脚本。

【问题讨论】:

    标签: excel tabular format-conversion


    【解决方案1】:

    以下是可以使用的示例 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 jars

    【讨论】:

    • 嗨,CTTableStyleInfo table_style = cttable.addNewTableStyleInfo(); 的 c# 替代品是什么,因为我找不到 addNewTableStyleInfo() 函数。即使cttable.tableStyleInfo=new CT_TableStyleInfo(); 这也行不通。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-26
    • 1970-01-01
    • 2019-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-24
    相关资源
    最近更新 更多