Open XML操作Excel实现单元格的合并.docx_第1页
Open XML操作Excel实现单元格的合并.docx_第2页
Open XML操作Excel实现单元格的合并.docx_第3页
Open XML操作Excel实现单元格的合并.docx_第4页
Open XML操作Excel实现单元格的合并.docx_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Open XML操作Excel实现单元格的合并1、引入必要的lib文件以下代码是建立在Open XML SDK CTP 2基础上的,使用前请先下载Open XML Format SDK 2.0。SDK默认会安装在C:Program Files (x86)Open XML Format SDKV2.0 (64bit)目录下,lib子目录下的DocumentFormat.OpenXml.dll必须被引用到项目中。添加对WindowsBase的引用2、 获取 SpreadsheetDocument 对象在 Open XML SDK 中,SpreadsheetDocument 类表示 Excel 文档包。若要打开并使用 Excel 文档,请基于文档创建 SpreadsheetDocument 类的一个实例。基于文档创建实例后,即可获取对包含工作表的主工作簿部件的访问权限。在此包中,使用 SpreadsheetML 标记将文档中的文本表示为 XML 形式。若要从文档中创建类实例,请调用 Open 重载方法之一。提供了多个方法,每个方法都有不同的签名。本主题中的示例代码使用带有需要两个参数的签名的 Open(String, Boolean) 方法。第一个参数采用表示要打开的文档的完整路径字符串。第二个参数是 true 或 false,表示是否要打开文件以进行编辑。如果此参数为 false,则不会保存对该文档所做的任何更改。下面的 using 语句中显示了调用 Open 方法的代码。/ Open the document for editing.using (SpreadsheetDocument document = SpreadsheetDocument.Open(docName, true) / Insert other code here.3、 获取 Worksheet 对象private static Worksheet GetWorksheet(SpreadsheetDocument document, string worksheetName) IEnumerable sheets = document.WorkbookPart.Workbook.Descendants().Where(s = s.Name = worksheetName); WorksheetPart worksheetPart = (WorksheetPart)document.WorkbookPart.GetPartById(sheets.First().Id) if (sheets.Count() = 0) return null; else return worksheetPart.Worksheet;4、 获取列名称代码会新建一个正则表达式以匹配单元格名称的列名称部分。此正则表达式匹配任何大写字母或小写字母的组合。代码通过调用 Regex.Match 获取列名称。private static string GetColumnName(string cellName) / Create a regular expression to match the column name portion of the cell name. Regex regex = new Regex(A-Za-z+); Match match = regex.Match(cellName); return match.Value;5、获取行索引为了获取行索引,代码会新建一个正则表达式以匹配单元格名称的行索引部分。此正则表达式匹配任何十进制数字组合。下面的代码示例创建一个正则表达式以匹配单元格名称的行索引部分(由十进制数字组成)。private static uint GetRowIndex(string cellName) / Create a regular expression to match the row index portion the cell name. Regex regex = new Regex(d+); Match match = regex.Match(cellName); return uint.Parse(match.Value);6、验证单元格是否已创建,如果没有就创建验证指定的单元格是否存在,如果不存在,则通过调用 CreateSpreadsheetCellIfNotExist 方法来创建单元格,并将其追加到适当的 Row 对象。private static void CreateSpreadsheetCellIfNotExist(Worksheet worksheet, string cellName) string columnName = GetColumnName(cellName); uint rowIndex = GetRowIndex(cellName); IEnumerable rows = worksheet.Descendants().Where(r = r.RowIndex.Value = rowIndex); / If the Worksheet does not contain the specified row, create the specified row. / Create the specified cell in that row, and insert the row into the Worksheet. if (rows.Count() = 0) Row row = new Row() RowIndex = new UInt32Value(rowIndex) ; Cell cell = new Cell() CellReference = new StringValue(cellName) ; row.Append(cell); worksheet.Descendants().First().Append(row); worksheet.Save(); else Row row = rows.First(); IEnumerable cells = row.Elements().Where(c = c.CellReference.Value = cellName); / If the row does not contain the specified cell, create the specified cell. if (cells.Count() = 0) Cell cell = new Cell() CellReference = new StringValue(cellName) ; row.Append(cell); worksheet.Save(); 7、 合并单元格下面的代码合并 SpreadsheetDocument 文档包中的两个相邻单元格。当合并两个单元格时,只会保留其中一个单元格中的内容。在从左到右的语言中,保留左上方单元格中的内容。在从右到左的语言中,保留右上方单元格中的内容。可以通过使用以下代码示例在程序中调用 MergeTwoCells 方法,该方法合并“Sheet9.xlsx”文件中名为“Jane”的工作表中的两个单元格 B2 和 C2。string docName = C:UsersPublicDocumentsSheet9.xlsx;string sheetName = Jane;string cell1Name = B2;string cell2Name = C2;MergeTwoCells(docName, sheetName, cell1Name, cell2Name);以下是使用 合并代码:private static void MergeTwoCells(string docName, string sheetName, string cell1Name, string cell2Name) / Open the document for editing. using (SpreadsheetDocument document = SpreadsheetDocument.Open(docName, true) Worksheet worksheet = GetWorksheet(document, sheetName); if (worksheet = null | string.IsNullOrEmpty(cell1Name) | string.IsNullOrEmpty(cell2Name) return; / Verify if the specified cells exist, and if they do not exist, create them. CreateSpreadsheetCellIfNotExist(worksheet, cell1Name); CreateSpreadsheetCellIfNotExist(worksheet, cell2Name); MergeCells mergeCells; if (worksheet.Elements().Count() 0) mergeCells = worksheet.Elements().First(); else mergeCells = new MergeCells(); / Insert a MergeCells object into the specified position. if (worksheet.Elements().Count() 0) worksheet.InsertAfter(mergeCells, worksheet.Elements().First(); else if (worksheet.Elements().Count() 0) worksheet.InsertAfter(mergeCells, worksheet.Elements().First(); else if (worksheet.Elements().Count() 0) worksheet.InsertAfter(mergeCells, worksheet.Elements().First(); else if (worksheet.Elements().Count() 0) worksheet.InsertAfter(mergeCells, worksheet.Elements().First(); else if (worksheet.Elements().Count() 0) worksheet.InsertAfter(mergeCells, worksheet.Elements().First(); else if (worksheet.Elements().Count() 0) worksheet.InsertAfter(mergeCells, worksheet.Elements().First(); else if (worksheet.Elements().Count() 0) worksheet.InsertAfter(mergeCells, worksheet.Elements().First(); else if (worksheet.Elements().Count() 0) worksheet.InsertAfter(mergeCells, worksheet.Elements().First(); else worksheet.InsertAfter

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论