0

我编写了一个方法来在传递文件名、工作表名和单元格地址时返回 Excel 工作表单元格值。

public static string GetCellValue(string fileName, string sheetName, string addressName)
        {
            string value = null;


            using (SpreadsheetDocument document = SpreadsheetDocument.Open(fileName, false))
            {

                WorkbookPart wbPart = document.WorkbookPart;


                  Where(s => s.Name == sheetName).FirstOrDefault();


                if (theSheet == null)
                {
                    throw new ArgumentException("sheetName");
                }


                WorksheetPart wsPart =
                    (WorksheetPart)(wbPart.GetPartById(theSheet.Id));


                Cell theCell = wsPart.Worksheet.Descendants<Cell>().
                  Where(c => c.CellReference == addressName).FirstOrDefault();


                if (theCell != null)
                {
                    value = theCell.InnerText;


                    if (theCell.DataType != null)
                    {
                        switch (theCell.DataType.Value)
                        {
                            case CellValues.SharedString:
.
                                var stringTable =
                                    wbPart.GetPartsOfType<SharedStringTablePart>()
                                    .FirstOrDefault();

                                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;
        }

这是调用上述方法的方法。

 private void button1_Click(object sender, EventArgs e)
        {
            const string fileName = @"C:\Users\dkumarage\Desktop\Book1.xlsx";
            string value = GetCellValue(fileName, "Sheet1", "A1");
            textBox1.Text = value;
        }

这样我只能获得 1 个单元格值。如何递归调用此方法以获取所有单元格值。请帮我。(在这里我不能通过范围。因为我不知道范围。相反我必须使用 while(不是行尾))

谢谢你

4

1 回答 1

1

我不知道你为什么希望它是递归的。无论如何,这是您想要做的非递归版本。它使用 Excel 互操作,我没有费心去管理异常(比如找不到文件,或者工作表不存在等等),但它应该给你一个起点。

public static List<string> GetCellValue(string fileName, string sheetName, string addressName)
    {
        List<string> result = new List<String>;
        object hmissing = System.Reflection.Missing.Value;
        Application app = new ApplicationClass();
        Workbook aWb = app.Workbooks.Open(fileName, hmissing, hmissing, hmissing, hmissing,
            hmissing, hmissing, hmissing, hmissing, hmissing, hmissing, hmissing, hmissing, hmissing,
            hmissing);
        Worksheet aWs = aWb.Worksheets[sheetName] as Worksheet;
        object[,] values = aWs.UsedRange.get_Value(hmissing) as object[,];
        foreach (object anObj in values)
             if (anObj != null)
                 result.Add(anObj.ToString());  
        aWb.Close(false, hmissing, hmissing);
        app.Quit();          
        return result;
    }
于 2012-06-12T09:47:50.773 回答