word ppt excel 后缀加rar 都可以解压看到资源文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using System.Text.RegularExpressions;namespace OpenXmlTest
{class TLExcel{#region 全局属性及方法public static int TLOK = 0;public static int TLERR = -1;public static void ShowMessage(string strMsg){Console.WriteLine(strMsg);}#endregion#region 私有属性private SpreadsheetDocument _spreadsheetDocument;#endregionpublic TLExcel(){ }/* 打开EXCEL */public int Open(string szPath) {try{_spreadsheetDocument = SpreadsheetDocument.Open(szPath, true);if (null == _spreadsheetDocument)return TLERR;return TLOK;}catch (Exception ex){ShowMessage("[Func:Open], Exception:" + ex.Message);return TLERR;}}/* 创建EXCEL */public int Create(string szPath){try{_spreadsheetDocument = SpreadsheetDocument.Create(szPath, SpreadsheetDocumentType.Workbook);// Add a WorkbookPart to the document.WorkbookPart workbookpart = _spreadsheetDocument.AddWorkbookPart();workbookpart.Workbook = new Workbook();// Add a WorksheetPart to the WorkbookPart.WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();worksheetPart.Worksheet = new Worksheet(new SheetData());// Add Sheets to the Workbook.Sheets sheets = _spreadsheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());// Append a new worksheet and associate it with the workbook.Sheet sheet = new Sheet() { Id = _spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Sheet1" };sheets.Append(sheet);workbookpart.Workbook.Save();return TLOK;}catch (Exception ex){ShowMessage("[Func:Create], Exception:" + ex.Message);return TLERR;}}/* 保存Excel */public void Save(){try{_spreadsheetDocument.WorkbookPart.Workbook.Save();}catch (Exception ex){ShowMessage("[Func:Save],Exception:" + ex.Message);}}/* 关闭Excel */public void Close(){try{this.Save();    //先保存_spreadsheetDocument.Close();}catch (Exception ex){ShowMessage("[Func:Save],Exception:" + ex.Message);}}#region 读写EXCEL/// <summary>/// 读取单元格数据/// </summary>/// <param name="iSheetNum">Sheet编号</param>/// <param name="iRowNum">行号</param>/// <param name="strColName">列名</param>/// <param name="iColNum">返回的单元格数据</param>/// <returns>状态</returns>public int ReadCellValue(int iSheetNum, uint iRowNum, string strColName, ref string strValue){Worksheet sheet = GetSpreadsheetWorksheet(iSheetNum);if (null == sheet){return TLERR;}Cell cell = GetSpreadsheetCell(sheet, strColName, iRowNum);if (null == cell){strValue = null;return TLERR;}string value = cell.CellValue.InnerText;SharedStringTablePart shareStringPart = this.GetSharedStringTable();strValue = shareStringPart.SharedStringTable.ChildElements[Int32.Parse(value)].InnerText;return TLOK;}//写入数据public int WriteCellValue(int iSheetNum, uint iRowNum, string strColName, string strValue){//bool bIsNewCell = false;    //是否是新单元格Worksheet sheet = GetSpreadsheetWorksheet(iSheetNum);if (null == sheet){return TLERR;}// Get the SharedStringTablePart and add the result to it.// If the SharedStringPart does not exist, create a new one.SharedStringTablePart shareStringPart = this.GetSharedStringTable();Cell cell = GetSpreadsheetCell(sheet, strColName, iRowNum);if (null == cell){//插入一个单元格cell = InsertCellInWorksheet(strColName, iRowNum, sheet);int iIndex = InsertSharedStringItem(strValue, shareStringPart);// Set the value of the cell.cell.CellValue = new CellValue(iIndex.ToString());cell.DataType = new EnumValue<CellValues>(CellValues.SharedString);}else{int iIndex;if (null == cell.CellValue){cell.CellValue = new CellValue();iIndex = InsertSharedStringItem(strValue, shareStringPart);cell.CellValue.Text = iIndex.ToString();}else { iIndex = Int32.Parse(cell.CellValue.Text);}OpenXmlElement xmlElement = shareStringPart.SharedStringTable.ChildElements[iIndex];((SharedStringItem)xmlElement).Text.Text = strValue;}return TLOK;}#endregion//获取行数public int GetRowsAndCols(int iSheetNum, ref int iRows){Worksheet workSheet = GetSpreadsheetWorksheet(iSheetNum);if (null == workSheet){return TLERR;}IEnumerable<Row> rows = workSheet.Descendants<Row>();iRows = rows.Count();return TLOK;}//获取Sheet数目public int GetWorksheetNumber(){IEnumerable<Sheet> sheets = _spreadsheetDocument.WorkbookPart.Workbook.Descendants<Sheet>();return sheets.Count();}//获取sheetNamepublic int GetWorksheetName(int iSheetNum, ref string strSheetName){IEnumerable<Sheet> sheets = _spreadsheetDocument.WorkbookPart.Workbook.Descendants<Sheet>().Where(s => s.SheetId == iSheetNum);if (1 != sheets.Count()){ShowMessage("[Func:ReadCellValue], 指定的Sheet不存在");return TLERR;}strSheetName = sheets.First().Name;return TLOK;}private Sheet GetSheet(int iSheetNum){IEnumerable<Sheet> sheets = _spreadsheetDocument.WorkbookPart.Workbook.Descendants<Sheet>().Where(s => s.SheetId == iSheetNum);if (1 != sheets.Count()){ShowMessage("[Func:ReadCellValue], 指定的Sheet不存在");return null;}return sheets.First();}private Sheet GetSheet(string strSheetName){IEnumerable<Sheet> sheets = _spreadsheetDocument.WorkbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == strSheetName);if (1 != sheets.Count()){ShowMessage("[Func:ReadCellValue], 指定的Sheet不存在");return null;}return sheets.First();}private SharedStringTablePart GetSharedStringTable(){SharedStringTablePart shareStringPart;if (_spreadsheetDocument.WorkbookPart.GetPartsOfType<SharedStringTablePart>().Count() > 0){shareStringPart = _spreadsheetDocument.WorkbookPart.GetPartsOfType<SharedStringTablePart>().First();}else{shareStringPart = _spreadsheetDocument.WorkbookPart.AddNewPart<SharedStringTablePart>();}return shareStringPart;}//获取workSheetprivate Worksheet GetSpreadsheetWorksheet(int iSheetNum){Sheet sheet = GetSheet(iSheetNum);if (null == sheet)return null;WorksheetPart worksheetPart = (WorksheetPart)_spreadsheetDocument.WorkbookPart.GetPartById(sheet.Id);Worksheet worksheet = worksheetPart.Worksheet;return worksheet;}//获取workSheetprivate Worksheet GetSpreadsheetWorksheet(string strSheetName){Sheet sheet = GetSheet(strSheetName);if (null == sheet)return null;WorksheetPart worksheetPart = (WorksheetPart)_spreadsheetDocument.WorkbookPart.GetPartById(sheet.Id);Worksheet worksheet = worksheetPart.Worksheet;return worksheet;}// Given a worksheet, a column name, and a row index, gets the cell at the specified column and row.private static Cell GetSpreadsheetCell(Worksheet worksheet, string columnName, uint rowIndex){IEnumerable<Row> rows = worksheet.GetFirstChild<SheetData>().Elements<Row>().Where(r => r.RowIndex == rowIndex);if (rows.Count() == 0){// A cell does not exist at the specified row.return null;}IEnumerable<Cell> cells = rows.First().Elements<Cell>().Where(c => string.Compare(c.CellReference.Value, columnName + rowIndex, true) == 0);if (cells.Count() == 0){// A cell does not exist at the specified column, in the specified row.return null;}return cells.First();}// Given a column name, a row index, and a WorksheetPart, inserts a cell into the worksheet. // If the cell already exists, returns it. private static Cell InsertCellInWorksheet(string columnName, uint rowIndex, Worksheet worksheet/*WorksheetPart worksheetPart*/){//Worksheet worksheet = worksheetPart.Worksheet;SheetData sheetData = worksheet.GetFirstChild<SheetData>();string cellReference = columnName + rowIndex;// If the worksheet does not contain a row with the specified row index, insert one.Row row;if (sheetData.Elements<Row>().Where(r => r.RowIndex == rowIndex).Count() != 0){row = sheetData.Elements<Row>().Where(r => r.RowIndex == rowIndex).First();}else{row = new Row() { RowIndex = rowIndex };sheetData.Append(row);}// If there is not a cell with the specified column name, insert one.  if (row.Elements<Cell>().Where(c => c.CellReference.Value == columnName + rowIndex).Count() > 0){return row.Elements<Cell>().Where(c => c.CellReference.Value == cellReference).First();}else{// Cells must be in sequential order according to CellReference. Determine where to insert the new cell.Cell refCell = null;foreach (Cell cell in row.Elements<Cell>()){if (string.Compare(cell.CellReference.Value, cellReference, true) > 0){refCell = cell;break;}}Cell newCell = new Cell() { CellReference = cellReference };row.InsertBefore(newCell, refCell);worksheet.Save();return newCell;}}// Given text and a SharedStringTablePart, creates a SharedStringItem with the specified text // and inserts it into the SharedStringTablePart. If the item already exists, returns its index.private static int InsertSharedStringItem(string text, SharedStringTablePart shareStringPart){// If the part does not contain a SharedStringTable, create it.if (shareStringPart.SharedStringTable == null){shareStringPart.SharedStringTable = new SharedStringTable();}int i = 0;foreach (SharedStringItem item in shareStringPart.SharedStringTable.Elements<SharedStringItem>()){if (item.InnerText == text){// The text already exists in the part. Return its index.return i;}i++;}// The text does not exist in the part. Create the SharedStringItem.shareStringPart.SharedStringTable.AppendChild(new SharedStringItem(new DocumentFormat.OpenXml.Spreadsheet.Text(text)));shareStringPart.SharedStringTable.Save();return i;}}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using System.Text.RegularExpressions;
using Excel = Microsoft.Office.Interop.Excel;namespace OpenXmlTest
{class Program{public static void UseCommom(){Excel.Application excelApp = new Excel.Application();Excel.Workbook expWorkBookConfig = null;//EXCEL 产品配置文件 (中文)Excel.Workbooks expWorkBooks = null;//打开Excle文件 创建表格对象excelApp.Visible = false; excelApp.DisplayAlerts = false;try{expWorkBooks = excelApp.Workbooks;expWorkBookConfig = expWorkBooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);Excel.Worksheet sheet = expWorkBookConfig.Worksheets[1];for (int iLine = 1; iLine < 200; iLine++){for (char ch = 'A'; ch <= 'Z'; ch++){string str = string.Format("{0}", ch);Excel.Range ACell = sheet.get_Range(str + iLine.ToString(), System.Reflection.Missing.Value);ACell.Value2 = "TestTestTestTestTestTestTestTestTestTestTestTestTestTestTest";}}expWorkBookConfig.SaveAs(@"D:\OpenXmlTest\bin\Release\Commom.xlsx",System.Reflection.Missing.Value,System.Reflection.Missing.Value,System.Reflection.Missing.Value,System.Reflection.Missing.Value,System.Reflection.Missing.Value,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,System.Reflection.Missing.Value,System.Reflection.Missing.Value,System.Reflection.Missing.Value,System.Reflection.Missing.Value,System.Reflection.Missing.Value);expWorkBookConfig.Close();excelApp.Quit();}catch (System.Exception ex){Console.WriteLine(ex.Message);excelApp.Quit();expWorkBookConfig = null;excelApp = null;}GC.Collect();}public static void UseOpenXml(){TLExcel doc = new TLExcel();if (TLExcel.TLOK == doc.Create("OpenXml.xlsx")){Console.WriteLine("开始时间:" + DateTime.Now);for (int iLine = 1; iLine < 200; iLine++){for (char ch = 'A'; ch <= 'Z'; ch++){string str = string.Format("{0}", ch);doc.WriteCellValue(1, (uint)iLine, str, "TestTestTestTestTestTestTestTestTestTestTestTestTestTestTest");}}Console.WriteLine("结束时间:" + DateTime.Now);}doc.Close();}static void Main(string[] args){Console.WriteLine("OpenXml:");UseOpenXml();Console.WriteLine("Commom:");Console.WriteLine("开始时间:" + DateTime.Now);UseCommom();Console.WriteLine("结束时间:" + DateTime.Now);Console.ReadKey();}}
}

http://officeopenxml.com/anatomyofOOXML-xlsx.php

https://www.ecma-international.org/publications-and-standards/standards/ecma-376/

vsto c# openxml操作excel相关推荐

  1. 基于 DocumentFormat.OpenXml 操作 Excel (1)-- 初识

    最近抽空研究了一下 基于DocumentFormat.OpenXml操作Excel,也把自己的理解记录下来,便于日后可以查阅. 各种系统中,导出Excel是一种很常见的功能,在C#/.Net 环境下, ...

  2. OpenXml 操作Excel,Word,PPT

    转载自线上数据建模 (allmlp.com) OpenXml 可以在不用打开Excel,Word和PPT的情况下,对其进行操作. 我们知道Excel,PPT和word可以通过对其进行解压,发现所有的数 ...

  3. Open XML操作Excel导入数据

    项目中发现使用OleDb(using System.Data.OleDb)相关对象处理Excel导入功能,不是很稳定经常出问题,需要把这个问题解决掉.项目组提出使用OpenXML来处理Excel的导入 ...

  4. VSTO C# 操作excel

    VSTO C# 操作excel using System; using System.Data; using System.Configuration; using System.Web; using ...

  5. PHP读写操作Excel

    PHP操作Excel最好的方法是使用PHPExcel类, 可以到官网下载PHPExcel类库 http://phpexcel.codeplex.com 国外网站有时候访问不了,  所以我上传了到CSD ...

  6. PHP操作excel类 PHPExcel

    PHP操作excel类 PHPExcel http://www.cr173.com/soft/40741.html   我的微云:http://share.weiyun.com/2db79f1438f ...

  7. net 操作 EXCEL

    .net 操作 EXCEL 背景:   在项目中,需要操作EXCEL 文档,本以为是OBA的应用,但其实不然.OBA是在EXCEL中嵌入.net应用插件,而我们则是需要在SCSF中操作EXCEL.   ...

  8. microsoft query sql引用单元格_Microsoft.Office.Interop.Excel操作Excel拾遗

    背景: 最近用C#写了一个小工具需要操作Excel,网上搜到的方案都是COM组件方式,即Microsoft.Office.Interop.Excel居多,除此之外,还有以下几种方法: Microsof ...

  9. .NET/C#使用NPOI操作Excel

    前言 Asp.net/C#操作Excel最惨的就是环境配置了:使用NPOI能够帮助开发者在没有安装微软Office的情况下读写Office 97-2003的文件,支持的文件格式包括xls, doc, ...

  10. C#使用oledb操作excel文件的方法

    本文实例讲述了C#使用oledb操作excel文件的方法.分享给大家供大家参考.具体分析如下: 不管什么编程语言都会提供操作Excel文件的方式,C#操作Excel主要有以下几种方式: 1.Excel ...

最新文章

  1. ElasticSearch是什么?为什么快?倒排索引是什么?ElasticSearch的应用?
  2. asp.net HC架构 在.netCore上的配置
  3. java中i++和++i与c里的区别
  4. 汇编语言--常见转移指令
  5. 【黑马程序员 C++教程从0到1入门编程】【笔记3】C++核心编程(内存分区模型、引用、函数提高)
  6. 谈谈你对云计算技术的看法
  7. BZOJ3944: Sum
  8. C++ Tricks
  9. 因此,Oracle杀死了java.net
  10. Windows10 安装 RabbitMQ
  11. 决策树-剪枝算法(二)
  12. java index.jsp为什么不默认跳转_Java开发人员怎么面试 常见Redis面试题有哪些
  13. OSChina 周二乱弹 —— 我国领先世界的IT技术
  14. 学生优化--文本框限制
  15. 计算机学的是苹果系统,苹果电脑装windows7教程 苹果电脑装windows7方法
  16. openStack开源云repo db local or on-line 实战部署之Ruiy王者归来
  17. 圆的内接正n边形的周长
  18. Unity插件 - MeshEditor(二) 模型网格编辑器(高级)
  19. HtmlHelp调用chm帮助文档使用
  20. 用matlab求二重积分例题_数学建模matlab例题参考及练习

热门文章

  1. 2009年英国大学综合排名
  2. 数学符号大全(量词符号、代数符号等)
  3. AUTOCAD——直线命令
  4. Java中IO流详细整合(含案例)
  5. Spring+Struts2+Hibernate概述
  6. Java整数的所有质因数,用JAVA将一个正整数分解成质因数,例如输入90,打印出90=2*3*3*5...
  7. ug录入属性_ug表格属性
  8. easypoi 批量导出_浅谈easypoi快速实现excel批量导入
  9. PC网站接入微信登陆流程一:微信开放平台账号注册和开发者资质认证
  10. Unity3D游戏开发之路:一月工作总结