简体   繁体   English

在C#OpenXML中读取.xlsx

[英]Reading .xlsx in C# OpenXML

I'm trying to see if it's possible to read a .xlsx file via OpenXML into a gridview with C# 我正在尝试查看是否有可能使用C#通过OpenXML将.xlsx文件读取到gridview中

I have done it via the OleDB connection but I want to experiment with using OpenXML. 我已经通过OleDB连接完成了此操作,但是我想尝试使用OpenXML。 Is gridview the way to go with this or is there some otherway I should be handling the data it gives out? 是gridview的解决方法还是我应该处理它给出的数据?

Also if anyone has any further reading material on Importing with OpenXML that would be handy as the web seems pretty sparse at the moment. 另外,如果有人对使用OpenXML导入有任何进一步的阅读材料,那将是非常方便的,因为当前网络似乎非常稀疏。

Editing: 编辑:

So after the link below I have come back with this: 因此,在下面的链接之后,我又回来了:

  using (SpreadsheetDocument myWorkbook = SpreadsheetDocument.Open(xlsxFilePath, true))
            {
                //Access the main Workbook part, which contains data

                WorkbookPart workbookPart = myWorkbook.WorkbookPart;

                WorksheetPart worksheetPart = null;

                if (!string.IsNullOrEmpty(sheetName))
                {
                    Sheet ss = workbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == sheetName).SingleOrDefault<Sheet>();

                    worksheetPart = (WorksheetPart)workbookPart.GetPartById(ss.Id);
                }
                else
                {
                    worksheetPart = workbookPart.WorksheetParts.FirstOrDefault();
                }

                SharedStringTablePart stringTablePart = workbookPart.SharedStringTablePart;

                if (worksheetPart != null)
                {
                    Row lastRow = worksheetPart.Worksheet.Descendants<Row>().LastOrDefault();

                    Row firstRow = worksheetPart.Worksheet.Descendants<Row>().FirstOrDefault();

                    if (firstRow != null)
                    {
                        foreach (Cell c in firstRow.ChildElements)
                        {
                            string value = GetValue(c, stringTablePart);

                            dt.Columns.Add(value);
                        }
                    }

                    if (lastRow != null)
                    {
                        for (var i = 2; i <= lastRow.RowIndex; i++)
                        {
                            DataRow dr = dt.NewRow();

                            var empty = true;

                            Row row = worksheetPart.Worksheet.Descendants<Row>().Where(r => i == r.RowIndex).FirstOrDefault();

                            var j = 0;

                            if (row != null)
                            {
                                foreach (Cell c in row.ChildElements)
                                {
                                    //Get cell value

                                    string value = GetValue(c, stringTablePart);

                                    if (!string.IsNullOrEmpty(value) && value != "")
                                    {
                                        empty = false;
                                    }

                                    dr[j] = value;

                                    j++;

                                    if (j == dt.Columns.Count)
                                    {
                                        break;
                                    }
                                }

                                if (empty)
                                {
                                    break;
                                }

                                dt.Rows.Add(dr);
                            }
                        }
                    }
                }
            }

            return dt;
        }

The problem that I am having is that I have to specify the sheet name - this may be beyond the remit of what's possible but is there an option to get the current sheet names and then have the user click which sheet they want the program to read - for example a pop-up combobox? 我遇到的问题是我必须指定工作表名称-这可能超出了可能,但是可以选择获取当前工作表名称,然后让用户单击他们希望程序读取的工作表-例如弹出式组合框?

to manipulate openxml in C# you can use the openxml SDK (available here: https://msdn.microsoft.com/en-us/library/office/bb448854.aspx ). 要在C#中操作openxml,您可以使用openxml SDK(在此处提供: https ://msdn.microsoft.com/zh-cn/library/office/bb448854.aspx)。

And for your problem, I don't think there is an embedded solution "Excel2Gridview" but manipulation shouldn't be very difficult. 对于您的问题,我认为没有嵌入式解决方案“ Excel2Gridview”,但操作起来应该不会很困难。 With a quick look on the net, you find the reverse question "gridview to Excel". 快速浏览一下网络,您会发现相反的问题“ gridview to Excel”。 So if you take a solution like this one https://social.msdn.microsoft.com/Forums/office/en-US/20d517d0-fb70-4879-b595-71b6d57be0ae/export-grid-view-to-excel-mysql?forum=exceldev apply the reverse method to translate your excel to gridview 因此,如果您采用这样的解决方案https://social.msdn.microsoft.com/Forums/office/zh-CN/20d517d0-fb70-4879-b595-71b6d57be0ae/export-grid-view-to-excel-mysql ?forum = exceldev应用反向方法将您的excel转换为gridview

update 更新

To retrieve sheets names, take a look at: https://msdn.microsoft.com/en-us/library/office/bb507946.aspx 要检索工作表名称,请查看: https : //msdn.microsoft.com/zh-cn/library/office/bb507946.aspx

To list sheetnames to users, you can create a form like: 要将工作表名称列出给用户,您可以创建如下形式:

  • Dialogbox to open the excel file 对话框打开Excel文件
  • Retrieve sheetnames 检索工作表名称
  • Propose the user a combobox with names 向用户建议一个名称为组合框
  • If "ok", execute your function with the chosen sheetname 如果“确定”,请使用所选的工作表名称执行功能

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

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