public class ExcelUntity
{ #region property
/// <summary>
/// excel文档(相当于excel程序)
/// </summary>
public SpreadsheetDocument spreadsheetDocument { get; set; } = null; /// <summary>
/// 工作本
/// </summary>
public WorkbookPart workbookpart { get; set; } = null; /// <summary>
/// sheet集合
/// </summary>
public Sheets sheets { get; set; } = null;
#endregion public SpreadsheetDocument Open(string filepath,bool isEdit)
{
spreadsheetDocument = SpreadsheetDocument.Open(filepath, isEdit);
return spreadsheetDocument;
} public SpreadsheetDocument Open(Stream stream, bool isEdit)
{
spreadsheetDocument = SpreadsheetDocument.Open(stream, isEdit);
return spreadsheetDocument;
} public void Close()
{
if (spreadsheetDocument != null)
spreadsheetDocument.Close();
}
} public static class ExtenOpenXML
{
#region Base /// <summary>
/// 获取Worksheet
/// </summary>
/// <param name="document">document对象</param>
/// <param name="sheetName">sheetName可空</param>
/// <returns>Worksheet对象</returns>
public static Worksheet GetWorksheet(this SpreadsheetDocument document, string sheetName = null)
{
var sheets = document.WorkbookPart.Workbook.Descendants<Sheet>();
var sheet = (sheetName == null
? sheets.FirstOrDefault()
: sheets.FirstOrDefault(s => s.Name == sheetName)) ?? sheets.FirstOrDefault(); var worksheetPart = (WorksheetPart)document.WorkbookPart.GetPartById(sheet.Id);
return worksheetPart.Worksheet;
} /// <summary>
/// 获了共享字符的表格对象
/// </summary>
/// <param name="document">SpreadsheetDocument</param>
/// <returns>SharedStringTablePart对角</returns>
public static IEnumerable<SharedStringTablePart> GetSharedStringTable(this SpreadsheetDocument document)
{
var sharedStringTable = document.WorkbookPart.GetPartsOfType<SharedStringTablePart>();
return sharedStringTable;
} /// <summary>
/// 获取第一个SheetData
/// </summary>
/// <param name="document">SpreadsheetDocument对象</param>
/// <param name="sheetName">sheetName可为空</param>
/// <returns>SheetData对象</returns>
public static SheetData GetFirstSheetData(this SpreadsheetDocument document, string sheetName = null)
{
return document.GetWorksheet(sheetName).GetFirstChild<SheetData>();
} /// <summary>
/// 获取第一个SheetData
/// </summary>
/// <param name="worksheet">Worksheet对象</param>
/// <returns>SheetData对象</returns>
public static SheetData GetFirstSheetData(this Worksheet worksheet)
{
return worksheet.GetFirstChild<SheetData>();
} /// <summary>
/// 获取WorkBook
/// </summary>
/// <param name="workBookPart"></param>
/// <returns></returns>
public static Workbook GetWorkbook(this WorkbookPart workBookPart)
{
return workBookPart.Workbook;
} /// <summary>
/// 修改单元格的内容.
/// </summary>
/// <param name="sheetData">
/// The sheet data.
/// </param>
/// <param name="cellName">
/// The cell name.
/// </param>
/// <param name="cellText">
/// The cell text.
/// </param>
public static void UpdateCellText(this SheetData sheetData, string cellName, string cellText)
{
var cell = sheetData.GetCell(cellName);
if (cell == null)
{
return;
}
cell.UpdateCellText(cellText); } /// <summary>
/// 修改单元格的文本
/// </summary>
/// <param name="cell">Cell对象</param>
/// <param name="cellText">文本字符串</param>
private static void UpdateCellText(this DocumentFormat.OpenXml.Spreadsheet.Cell cell, object cellText)
{
cell.DataType = GetCellDataType(cellText);
cell.CellValue = cell.CellValue ?? new CellValue();
cell.CellValue.Text = cellText.ToString();
} /// <summary>
/// The get cell data type.
/// </summary>
/// <param name="cellText">
/// The cell text.
/// </param>
/// <returns>
/// The <see cref="CellValues"/>.
/// </returns>
private static CellValues GetCellDataType(object cellText)
{
var type = cellText.GetType();
switch (type.Name)
{
case "Int32":
case "Decimal":
case "Double":
case "Int64":
return CellValues.Number;
case "String":
return CellValues.String;
//// case "DateTime":
//// return CellValues.Date;
default:
return CellValues.String;
}
} /// <summary>
/// 修改单元格内容(文本、样式)
/// </summary>
/// <param name="cell">
/// The cell.
/// </param>
/// <param name="cellText">
/// The cell text.
/// </param>
/// <param name="cellStyleIndex">
/// The cell style index.
/// </param>
private static void UpdateCell(this DocumentFormat.OpenXml.Spreadsheet.Cell cell, object cellText, uint cellStyleIndex)
{
cell.UpdateCellText(cellText);
cell.StyleIndex = cellStyleIndex;
} /// <summary>
/// 根据单元格名称获取行索引.
/// </summary>
/// <param name="cellName">
/// The cell name.
/// </param>
/// <returns>
/// The <see cref="uint"/>.
/// </returns>
private static uint GetRowIndex(string cellName)
{
var regex = new Regex(@"\d+");
var match = regex.Match(cellName);
return uint.Parse(match.Value);
} /// <summary>
/// 获取行
/// </summary>
/// <param name="sheetData">
/// The sheet data.
/// </param>
/// <param name="rowIndex">
/// The row index.
/// </param>
/// <returns>
/// The <see cref="Row"/>.
/// </returns>
private static DocumentFormat.OpenXml.Spreadsheet.Row GetRow(this SheetData sheetData, long rowIndex)
{
return sheetData.Elements<DocumentFormat.OpenXml.Spreadsheet.Row>().FirstOrDefault(r => r.RowIndex == rowIndex);
} /// <summary>
/// 获取单元格
/// </summary>
/// <param name="row">
/// The row.
/// </param>
/// <param name="cellName">
/// The cell name.
/// </param>
/// <returns>
/// The <see cref="Cell"/>.
/// </returns>
private static DocumentFormat.OpenXml.Spreadsheet.Cell GetCell(this DocumentFormat.OpenXml.Spreadsheet.Row row, string cellName)
{
return row.Elements<DocumentFormat.OpenXml.Spreadsheet.Cell>().FirstOrDefault(c => c.CellReference.Value == cellName);
} /// <summary>
/// 获取单元格
/// </summary>
/// <param name="sheetData">
/// The sheet data.
/// </param>
/// <param name="cellName">
/// The cell name.
/// </param>
/// <returns>
/// The <see cref="Cell"/>.
/// </returns>
private static DocumentFormat.OpenXml.Spreadsheet.Cell GetCell(this SheetData sheetData, string cellName)
{
return sheetData.Descendants<DocumentFormat.OpenXml.Spreadsheet.Cell>().FirstOrDefault(c => c.CellReference.Value == cellName);
} /// <summary>
/// 获取区域信息
/// </summary>
/// <param name="workbook"></param>
/// <returns></returns>
public static DefinedNames GetDefinedNames(this Workbook workbook)
{
return workbook.DefinedNames;//区域名称集合
} /// <summary>
/// 取单元格的值
/// </summary>
/// <param name="RowData"></param>
/// <param name="columnCode"></param>
/// <param name="sharedStringTablePart"></param>
/// <returns></returns>
public static string GetCellValue(this Row RowData,string columnCode,SharedStringTablePart sharedStringTablePart)
{
string value = string.Empty;
try
{
Cell cell = RowData.GetCell(columnCode);//取单元格
if (cell != null)
value = GetCellValue(cell, sharedStringTablePart); value = string.IsNullOrEmpty(value) ? "" : value;
}
catch(Exception ex)
{
throw ex;
}
return value;
} public static string GetCellValue(this Cell cell, SharedStringTablePart sharedStringTablePart)
{
if (cell == null)
return string.Empty; if(cell.ChildElements.Count ==)
return string.Empty; var value = cell.CellValue.InnerText;
if (cell.DataType == null)
return value; switch (cell.DataType.Value)
{
case CellValues.SharedString:
if (sharedStringTablePart != null)
value = sharedStringTablePart.SharedStringTable.ElementAt(int.Parse(value)).InnerText; break;
case CellValues.Boolean:
value = value == "" ? "FALSE" : "TRUE";
break;
}
return value;
}
#endregion }

openxml excel封装类的更多相关文章

  1. OpenXml Excel数据导入导出(含图片的导入导出)

    声明:里面的很多东西是基于前人的基础上实现的,具体是哪些人 俺忘了,我做了一些整合和加工 这个项目居于openxml做Excel的导入导出,可以用OpenXml读取Excel中的图片 和OpenXml ...

  2. php导出excel封装类

    因为实际情况的需要,导出excel表格在后台开发的过程中会经常用到.下面是我在实际应用中自己整理的一个导出excel类,需要PHPExcel支持,本类很好的完成导出表格的基本样式,保存路径,切换工作薄 ...

  3. csharp: Export DataTable to Excel using OpenXml 2.5 in asp.net

    //https://www.microsoft.com/en-us/download/details.aspx?id=5124 Open XML SDK 2.0 for Microsoft Offic ...

  4. 开源的读取Excel文件组件-ExcelDataReader

    ExcelDataReader可以读取 Microsoft Excel 文件 ('97-2007),支持Windows  .Net Framework 2 +. Windows Mobile with ...

  5. C# 读取Excel,一波华丽的操作

    C# 读取Excel,其实有很多方法.但是今天要来一波华丽的操作. 先看效果: 以上这波操作使用了 ExcelDataReader 和 ExcelDataReader.DataSet 完成的. Exc ...

  6. 读取excel的方法(可用于批量导入)

    FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read); //1. Reading from a binary ...

  7. 读excle

    1.OleDbConnection读取Excel ///<summary>///上传文件到临时目录中 ///</ummary>private void Upload(){ Ht ...

  8. .net core 读取Excal文件数据及注意事项

    添加ExcelDataReader.DataSet引用. 调用下列方法: public class XlsHelper { public static System.Data.DataSet GetX ...

  9. 一个用微软官方的OpenXml读写Excel 目前网上不太普及的方法。

    新版本的xlsx是使用新的存储格式,貌似是处理过的XML. 传统的excel处理方法,我真的感觉像屎.用Oldeb不方便,用com组件要实际调用excel打开关闭,很容易出现死. 对于OpenXML我 ...

随机推荐

  1. k8s学习笔记之五:Pod资源清单spec字段常用字段及含义

    第一章.前言 在上一篇博客中,我们大致简述了一般情况下资源清单的格式,以及如何获得清单配置的命令帮助,下面我们再讲解下清单中spec字段中比较常见的字段及其含义 第二章.常用字段讲解 spec.con ...

  2. leetcode每日刷题计划-简单篇day3

    收到swe提前批面试hhh算是ep挂了的后续 努力刷题呀争取今年冲进去! Num 21 合并两个有序链表 Merge Two Sorted Lists 注意新开的链表用来输出结果的是ListNode ...

  3. 【学习】DataFrame&Series类【pandas】

    参考链接:http://blog.csdn.net/yhb315279058/article/details/50226027 DataFrame类: DataFrame有四个重要的属性: index ...

  4. java细节知识

    代码优化细节 (1)尽量指定类.方法的final修饰符 带有final修饰符的类是不可派生的.在Java核心API中,有许多应用final的例子,例如java.lang.String,整个类都是fin ...

  5. DRF框架之 serializers 序列化组件

    1. 什么是序列化,其实在python中我们就学了序列化工具json工具,就是吧信息存为类字典形式 2. DRF框架自带序列化的工具: serializers 3. DRF框架 serializers ...

  6. java中封装类(一)

    java中封装类共九个,分别是Boolean,Byte,Short,Integer,Long,Float,Double,Character,Void 其中Void对于使用者并无多大意义,也不可以构造任 ...

  7. TCP与UDP,可靠UDP如何实现

    两种协议都是传输层协议,为应用层提供信息载体. TCP协议是基于连接的字节流的可靠协议,有三次握手,四次挥手,超时重传,流量控制(滑动窗口),拥塞控制和差错控制,也正因为有可靠性的保证和控制手段,所以 ...

  8. java.lang.RuntimeException: Canvas: trying to draw too large(203212800bytes) bitmap.

    https://www.cnblogs.com/spring87/p/7645625.html 今天我师父发现了一个问题:在更换登录页图片后,更新版本,部分手机打开会闪退.借了一个三星手机后,查看问题 ...

  9. cdn节点自定义防CC代码在哪里抄

    1. 登陆节点的3311后台 1. http redirect(普通) HTTP/1.1 302 FOUNDConnection: keep-aliveLocation: {{url}} <ht ...

  10. yum安装命令:遇到的问题报错如下: File "/usr/bin/yum", line 30 except KeyboardInterrupt, e: 通过看报错可以了解到是使用了python2的语法,所以了解到当前yum使用的Python2,因为我单独安装了python3,且python3设置为默认版本了,所以导致语法问题 解决方法: 使用python2.6 yum install

    1.安装zip yum install -y unzip zip 2.安装lrszs yum -y install lrzsz 3.安装scp 遇到下面的问题: 结果提示: No package sc ...