简体   繁体   English

用c#读取数据透视表

[英]Reading pivot table with c#

I am trying to read an excel-sheet containing a pivot table generated by macros with the following connection string: 我正在尝试阅读包含由宏生成的数据透视表的excel表,其中包含以下连接字符串:

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=path;
Extended Properties="Excel 8.0;HDR=NO;";

the problem is that the values doesn't appear in my dataset, only labels of columns and lines, but not necessary values. 问题是这些值不会出现在我的数据集中,只出现在列和行的标签上,而不是必需的值。

Any idea to read a such file? 有没有想过读这样的文件?

我不知道答案,但也许Epplus( http://epplus.codeplex.com/ )可以帮助你。

I don't know why, but by changing the provider to Provider=Microsoft.ACE.OLEDB.12.0 , data is read correctly. 我不知道为什么,但通过将提供Provider=Microsoft.ACE.OLEDB.12.0更改为Provider=Microsoft.ACE.OLEDB.12.0 ,可以正确读取数据。

the fill dataset is slower, i but I got what i'm looking for thanks 填充数据集较慢,但我得到了我正在寻找的感谢

This will read and excel table into data table in C#. 这将读取和excel表到C#中的数据表。 'sHeet' is the name of the sheet in Excel and startfromrow is where you want the table read to start from. 'sHeet'是Excel中工作表的名称,startfromrow是您希望从中读取表的位置。 excelTable is the Data Table where the data will be stored. excelTable是存储数据的数据表。

public void ReadEx(string sHeet, int startfromrow)
       {
          excelTable.Clear();
          string excelFile =  "filepath";
          Excel.Application excel = new Excel.Application();
          Excel.Workbook wb = excel.Workbooks.Open(excelFile);
          Excel.Worksheet pivotWorkSheet = (Excel.Worksheet)wb.Sheets[sHeet];
          Excel.Range xlRange = pivotWorkSheet.UsedRange;

          int rowCount = xlRange.Rows.Count;
          int colCount = xlRange.Columns.Count;


          if (excelTable.Columns.Count <= 0)
            {
              excelTable.Columns.Add("Row Labels");
              foreach (string column in lColumns)
              {
                  excelTable.Columns.Add(column);
              }
            }

          object[] exrow = new object[colCount];
          for (int i; startfromrow <= rowCount; row++)
          {
              for (int j = 1; j <= colCount; j++)
              {
                  exrow[j-1] = xlRange.Cells[startfromrow , j].Text;
                  Console.WriteLine(xlRange.Cells[startfromrow , j].Text);
              }
              excelTable.Rows.Add(exrow);
          }

I made some amendment on the answer from user3644719. 我对user3644719的答案做了一些修改。 In his/her answer, there are some typo, like variable not defined in the code. 在他/她的回答中,有一些拼写错误,比如代码中未定义的变量。

        String sHeet = "Pivot";

        DataTable excelTable = new DataTable();

        excelTable.Clear();
        string excelFile = "C:\\test_pivot.xlsx";
        Excel.Application excel = new Excel.Application();
        Excel.Workbook wb = excel.Workbooks.Open(excelFile);
        Excel.Worksheet pivotWorkSheet = (Excel.Worksheet)wb.Sheets[sHeet];
        Excel.Range xlRange = pivotWorkSheet.UsedRange;

        int rowCount = xlRange.Rows.Count;
        int colCount = xlRange.Columns.Count;

        string[] lColumns = new string[2] { "Column1","Column2" };

        if (excelTable.Columns.Count <= 0)
        {
            foreach (string column in lColumns)
            {
                excelTable.Columns.Add(column);
            }

        }

        Object[] exrow = new object[colCount];
        for (int row=1; row <= rowCount; row++)
        {
            for (int j = 1; j <= colCount; j++)
            {  
                exrow[j - 1] = xlRange.Cells[row, j].Text;
                Console.WriteLine(xlRange.Cells[row, j].Text);
            }

            excelTable.Rows.Add(exrow);
            //excelTable.Rows.Add(exrow);
        }

        String str = excelTable.ToString();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM