open xml excel read cell value

后端 未结 5 1738
感动是毒
感动是毒 2020-11-27 05:24

I am using the Open XML SDK to open an Excel xlsx file and I try to read the cellvalue on position A1 in each sheet. I use the following code:

using (Spreads         


        
5条回答
  •  攒了一身酷
    2020-11-27 06:01

    Found this very usefull snippet quite a while ago, so can not meantion the author.

    private static string GetCellValue(string fileName, string sheetName, string addressName)
        {
            string value = null;
    
            using(SpreadsheetDocument document =  SpreadsheetDocument.Open(fileName, false))
            {
                WorkbookPart wbPart = document.WorkbookPart;
    
                // Find the sheet with the supplied name, and then use that Sheet
                // object to retrieve a reference to the appropriate worksheet.
                Sheet theSheet = wbPart.Workbook.Descendants().
                  Where(s => s.Name == sheetName).FirstOrDefault();
    
                if(theSheet == null)
                {
                    throw new ArgumentException("sheetName");
                }
    
                // Retrieve a reference to the worksheet part, and then use its 
                // Worksheet property to get a reference to the cell whose 
                // address matches the address you supplied:
                WorksheetPart wsPart = (WorksheetPart)(wbPart.GetPartById(theSheet.Id));
                Cell theCell = wsPart.Worksheet.Descendants().
                  Where(c => c.CellReference == addressName).FirstOrDefault();
    
                // If the cell does not exist, return an empty string:
                if(theCell != null)
                {
                    value = theCell.InnerText;
    
                    // If the cell represents a numeric value, you are done. 
                    // For dates, this code returns the serialized value that 
                    // represents the date. The code handles strings and Booleans
                    // individually. For shared strings, the code looks up the 
                    // corresponding value in the shared string table. For Booleans, 
                    // the code converts the value into the words TRUE or FALSE.
                    if(theCell.DataType != null)
                    {
                        switch(theCell.DataType.Value)
                        {
                            case CellValues.SharedString:
                                // For shared strings, look up the value in the shared 
                                // strings table.
                                var stringTable = wbPart.
                                  GetPartsOfType().FirstOrDefault();
                                // If the shared string table is missing, something is 
                                // wrong. Return the index that you found in the cell.
                                // Otherwise, look up the correct text in the table.
                                if(stringTable != null)
                                {
                                    value = stringTable.SharedStringTable.
                                      ElementAt(int.Parse(value)).InnerText;
                                }
                                break;
    
                            case CellValues.Boolean:
                                switch(value)
                                {
                                    case "0":
                                        value = "FALSE";
                                        break;
                                    default:
                                        value = "TRUE";
                                        break;
                                }
                                break;
                        }
                    }
                }
            }
            return value;
        }
    

提交回复
热议问题