简体   繁体   English

你能像使用 OpenXML 的数据库行一样读取 Excel xlsx 行吗?

[英]Can you read an Excel xlsx row like a database row using OpenXML?

I just about lost my sanity yesterday with the disconnect between how easy this SHOULD be and the reality.昨天我差点失去理智,因为这应该是多么容易和现实之间脱节了。 The workbook is using shared strings;工作簿正在使用共享字符串; I did find a way to get the cell value but it'd be nicer if I could just get what's being displayed in the cell instead of jumping through hoops.我确实找到了一种获取单元格值的方法,但如果我可以只获取单元格中显示的内容而不是跳来跳去,那就更好了。 More importantly, I need to get the values of columns A, B, and C for each row but if the value is blank, it treats it like the cell doesn't exist?更重要的是,我需要为每一行获取 A、B 和 C 列的值,但如果该值为空,它会将其视为该单元格不存在吗? Can you fill in the pseudo code within the foreach Row statement, Bonus if there's a way to get the value directly.你能在foreach Row语句中填写伪代码吗,如果有一种方法可以直接获取值,则奖励。 but you can assume I have a method that will do all the string table lookup.但是你可以假设我有一个方法可以完成所有的字符串表查找。

try
{
    using (SpreadsheetDocument doc = SpreadsheetDocument.Open("C:\\temp\\sitemap.xlsx", false))
    {
        WorkbookPart workbookPart = doc.WorkbookPart;
        Sheets thesheetcollection = workbookPart.Workbook.GetFirstChild<Sheets>();
        StringBuilder excelResult = new StringBuilder();

        foreach (Sheet thesheet in thesheetcollection)
        {
            excelResult.AppendLine("Excel Sheet Name : " + thesheet.Name);
            excelResult.AppendLine("----------------------------------------------- ");

            Worksheet theWorksheet = ((WorksheetPart)workbookPart.GetPartById(thesheet.Id)).Worksheet;
            SheetData thesheetdata = (SheetData)theWorksheet.GetFirstChild<SheetData>();

            foreach (Row thecurrentrow in thesheetdata)
            {
                // Help needed here
                var val1 = thecurrentrow[0].InnerText; // Col A
                var val2 = thecurrentrow[1].InnerText; // Col B
                var val3 = thecurrentrow[2].InnerText; // Col C
            }
            excelResult.Append("");
            Console.WriteLine(excelResult.ToString());
            Console.ReadLine();
        }
    }
}
catch (Exception)
{

}

You will need to build all the cell addresses and then check if a cell with that address exists.您将需要构建所有单元格地址,然后检查是否存在具有该地址的单元格。 Like this:像这样:

    private void ReadExcel()
    {
        for (int rowIdx = 1; rowIdx < 100; rowIdx++)
        {
            for (int colIdx = 1; colIdx < 30; colIdx++)
            {
                string cellAdress = $"{GetExcelColumnName(colIdx)}{rowIdx}";

                DocumentFormat.OpenXml.Spreadsheet.Cell cell = theWorksheet.Descendants<DocumentFormat.OpenXml.Spreadsheet.Cell>().FirstOrDefault(c => c.CellReference == cellAdress);
                // if cell == null the cell has no value

            }
        }
    }

    private static string GetExcelColumnName(int columnNumber)
    {
        int dividend = columnNumber;
        string columnName = string.Empty;
        int modulo;

        while (dividend > 0)
        {
            modulo = (dividend - 1) % 26;
            columnName = Convert.ToChar(65 + modulo).ToString() + columnName;
            dividend = (dividend - modulo) / 26;
        }

        return columnName;
    }

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

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