如何使用openxml读取文件excel行和表特定? [英] How to read file excel row and sheet specific using openxml ?

查看:244
本文介绍了如何使用openxml读取文件excel行和表特定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用开放式XML C#将excel行和表特定读入sql server表?



我有一个没有格式的excel文件我的意思是我只能在第一张和第一行读取excel文件,我如何阅读第二张和第A6行? (列的名称从A6到D6开始)



我尝试使用oledb和interoop并且我读取打开xml是选择那个



你有例子,请新手和工作到很晚



我尝试了什么:



How to read excel row and sheet specific using open XML C# into sql server table ?

I have a excel file with no format I mean I can only read excel file in first sheet and first row, how i read second sheet and row A6? ( the names of a columns begin in A6 to D6)

I try with oledb and interoop and i read open xml is the choice for that

do you have examples , pls im newbie and late on the work

What I have tried:

protected void insertBoqElements_Click(object sender, EventArgs e)  
{  
    try  
    {  
        //specify the file name where its actually exist   
        string filepath = @ "D:\TPMS\Uploaded_Boq\test.xlsx";  
  
        //open the excel using openxml sdk  
        using(SpreadsheetDocument doc = SpreadsheetDocument.Open(filepath, false))  
        {  
  
            //create the object for workbook part  
            WorkbookPart wbPart = doc.WorkbookPart;  
  
            //statement to get the count of the worksheet  
            int worksheetcount = doc.WorkbookPart.Workbook.Sheets.Count();  
  
            //statement to get the sheet object  
            Sheet mysheet = (Sheet) doc.WorkbookPart.Workbook.Sheets.ChildElements.GetItem(0);  
  
            //statement to get the worksheet object by using the sheet id  
            Worksheet Worksheet = ((WorksheetPart) wbPart.GetPartById(mysheet.Id)).Worksheet;  
  
            //Note: worksheet has 8 children and the first child[1] = sheetviewdimension,....child[4]=sheetdata  
            int wkschildno = 4;  
  
  
            //statement to get the sheetdata which contains the rows and cell in table  
            SheetData Rows = (SheetData) Worksheet.ChildElements.GetItem(wkschildno);  
  
  
            //getting the row as per the specified index of getitem method  
            Row currentrow = (Row) Rows.ChildElements.GetItem(1);  
  
            //getting the cell as per the specified index of getitem method  
            Cell currentcell = (Cell) currentrow.ChildElements.GetItem(1);  
  
            //statement to take the integer value  
            string currentcellvalue = currentcell.InnerText;  
  
        }  
    } catch (Exception Ex)  
    {  
  
        lbldisplayerrors.Text = Ex.Message;  
    }  
  
}  

推荐答案

从这里开始:

< a href =https://docs.microsoft.com/en-us/office/open-xml/how-to-get-worksheet-information-from-a-package>如何:从Open获取工作表信息XML包(Open XML SDK)| Microsoft Docs [ ^ ]

如何:检索电子表格文档中的单元格值(Open XML SDK) Microsoft Docs [ ^ ]

如何:检索电子表格文档中的工作表列表(Open XML SDK) Microsoft Docs [ ^ ]
Start here:
How to: Get worksheet information from an Open XML package (Open XML SDK) | Microsoft Docs[^]
How to: Retrieve the values of cells in a spreadsheet document (Open XML SDK) | Microsoft Docs[^]
How to: Retrieve a list of the worksheets in a spreadsheet document (Open XML SDK) | Microsoft Docs[^]


这篇关于如何使用openxml读取文件excel行和表特定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆