zoukankan      html  css  js  c++  java
  • OpenXML读取Excel数据以及处理Excel大文件

    读取一个单元格的值,这个是官方的写法

    public string GetCellValue(string fileName, string sheetName, string addressName)
            {
                string value = null;
                try
                {
                    using (SpreadsheetDocument document = SpreadsheetDocument.Open(fileName, false))
                    {
                        WorkbookPart wbPart = document.WorkbookPart;
    
                        Sheet theSheet = wbPart.Workbook.Descendants<Sheet>().Where(s => s.Name == sheetName).FirstOrDefault();
    
                        if (theSheet == null)
                        {
                            throw new ArgumentException("sheetName" + sheetName);
                        }
    
                        WorksheetPart wsPart =
                            (WorksheetPart)(wbPart.GetPartById(theSheet.Id));
    
                        DocumentFormat.OpenXml.Spreadsheet.Cell theCell = wsPart.Worksheet.Descendants<DocumentFormat.OpenXml.Spreadsheet.Cell>().
                          Where(c => c.CellReference == addressName).FirstOrDefault();
    
                        if (theCell != null && theCell.InnerText.Length > 0)
                        {
                            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;
                                        }
                                        else
                                        {
                                            value = null;
                                        }
                                        break;
    
                                    case CellValues.Boolean:
                                        switch (value)
                                        {
                                            case "0":
                                                value = "FALSE";
                                                break;
                                            default:
                                                value = "TRUE";
                                                break;
                                        }
                                        break;
                                }
                            }
                        }
                        else
                        {
                            value = null;
                        }
                    }
                }
                catch (Exception ex)
                {
                    value = null;
                    AppGloab.SysLogger.Error(ex);
                }
    
                return value;
            }

    读取Excel大文件,这是根据官方读取大文件的方法改写的,可以读取到指定worksheet的值

    public virtual void ReadExcelFileDOM(string fileName, string worksheet = "")
            {
                try
                {
                    using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(fileName, false))
                    {
                        WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
                        WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
    
                        SheetData sheetData = new SheetData();
                        Sheet theSheet = workbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == worksheet).FirstOrDefault();
                        if (theSheet != null)
                        {
                            sheetData = ((WorksheetPart)workbookPart.GetPartById(theSheet.Id)).Worksheet.GetFirstChild<SheetData>();
                        }
                        int irow = 0;
                        foreach (DocumentFormat.OpenXml.Spreadsheet.Row r in sheetData.Elements<DocumentFormat.OpenXml.Spreadsheet.Row>())
                        {
                            irow = irow + 1;
                            AppGloab.SysLogger.Info("Worksheet Row:" + irow);
                            int icoloum = 0;
                            foreach (DocumentFormat.OpenXml.Spreadsheet.Cell c in r.Elements<DocumentFormat.OpenXml.Spreadsheet.Cell>())
                            {
                                icoloum = icoloum + 1;
                                AppGloab.SysLogger.Info("Worksheet  Row:" + irow + ", Column:" + icoloum);
                                string value = null;
                                if (c.InnerText.Length > 0)
                                {
                                    value = c.InnerText;
                                    if (c.DataType != null)
                                    {
                                        switch (c.DataType.Value)
                                        {
                                            case CellValues.SharedString:
                                                var stringTable =
                                                    workbookPart.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;
                                        }
                          Console.WriteLine(value); } } } } } }
    catch (Exception ex) { AppGloab.SysLogger.Error(ex); } }
  • 相关阅读:
    Android学习笔记【07】【四大组件之广播接收者】
    Android Adapter、Activity回传数据、发送短信
    Android学习笔记【06】【四大组件之Activity】
    Android学习笔记【05】【网络编程之二】
    Android学习笔记【04】【网络编程之一】
    Android学习笔记【03】【数据存储与数据展示】
    【转】sql server获取数据库名,表明,表结构
    C# 打开默认浏览器
    winform 重绘listbox
    字符串数组的简单应用,字符换行,c#,asp.net
  • 原文地址:https://www.cnblogs.com/yinxuejunfeng/p/14285678.html
Copyright © 2011-2022 走看看