【问题标题】:Linking of Specific excel columns via Open Xml通过 Open Xml 链接特定的 excel 列
【发布时间】:2016-09-09 09:03:57
【问题描述】:

使用Microsoft.Office.Interop.Excel.dll,我可以使用以下代码将特定行和特定列的数据从 Excel 工作表中获取到列表中

Excel.Workbook MyWorkBook = Excel_App.Workbooks.Open(path, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Excel.Worksheet MyWorksheet = null;
MyWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)MyWorkBook.Sheets[(1)];
Excel.Range Excel_Range;
Excel_Range = MyWorksheet.UsedRange;
SheetCount = MyWorkBook.Sheets.Count;
Lastrow = MyWorksheet.Cells.Find("*", System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlPrevious, false, System.Reflection.Missing.Value, System.Reflection.Missing.Value).Row;
LastColumn = MyWorksheet.Cells.Find("*", System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, Excel.XlSearchOrder.xlByColumns, Excel.XlSearchDirection.xlPrevious, false, System.Reflection.Missing.Value, System.Reflection.Missing.Value).Column;

for (int i = 8; i <= Lastrow; i++)
{


        List_MAPPING_FILE_A429_PATHS.Add((string)(Excel_Range.Cells[i, 4] as Excel.Range).Value2.ToString());
        List_MAPPING_FILE_ASCB_PATHS.Add((string)(Excel_Range.Cells[i, 5] as Excel.Range).Value2.ToString());

}

现在我想通过使用 OpenXml.dll 获取存储在列表中的相同数据,我尝试了下面的代码,但卡住了如何进一步进行

public void AddtoLogFile( )
{
    string temp =@"C:\Ported\DATA\EJETE2_A429RX_TIF_temp.xml";
    using (SpreadsheetDocument myDoc = SpreadsheetDocument.Open(temp, true))
    {
        WorkbookPart workbookPart = myDoc.WorkbookPart;
        WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
        SheetData sheetData =
        worksheetPart.Worksheet.Elements<SheetData>().First();
        foreach (Row r in sheetData.Elements<Row>())
        {
            foreach (Cell c in r.Elements<Cell>())
            {
                string text = c.CellValue.Text;
            }
        }
    }
}

有人可以帮助我吗?

【问题讨论】:

    标签: c# openxml


    【解决方案1】:

    Cells 位置存储在 Cell.CellReference 中,

    第一个单元格的单元格引用将是“A1”

    使用此方法提取列:

    private static readonly Regex ColumnNameRegex = new Regex("[A-Za-z]+");  
    private static string GetColumnName(string cellReference)
        {
            if (ColumnNameRegex.IsMatch(cellReference))
                return ColumnNameRegex.Match(cellReference).Value;
    
            throw new ArgumentOutOfRangeException(cellReference);
        }
    

    不确定你想从电子表格中得到什么,我猜你只想要特定行中单元格的信息:

     foreach (Row r in sheetData.Elements<Row>())
        {
            foreach (Cell c in r.Elements<Cell>())
            {
                if (GetColumnName(c.CellReference) == "A")
                {
                string text = c.CellValue.InnerText;
                }
            }
        }
    

    CellVaule.InnerText 提供的数据将是对包含工作表所有字符串的 SharedStringTabl 的引用。您需要从存储为

    的 SharedStringTable 获取数据

    WorkBookPart.SharedStringTablePart.SharedStringTable;

    为此,我使用了一种方法,该方法采用 Cell 和 SharedStringTable 来返回值:

            public static string GetCellV (Cell cell, SharedStringTable ss)
        {
            string cellV = null;
    
            try
            {
                cellV = cell.CellValue.InnerText;
                if (cell.DataType != null
                  && cell.DataType.Value == CellValues.SharedString)
                {
                    cellV = ss.ElementAt(Int32.Parse(cellV)).InnerText;
                }
                else
                {
                    cellV = cell.CellValue.InnerText;
                }
            }
            catch (Exception)
            {
                cellV = " ";
            }
    
            return cellV;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多