博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Open Xml 创建Excel并插入数据
阅读量:5985 次
发布时间:2019-06-20

本文共 3593 字,大约阅读时间需要 11 分钟。

创建Excel

private static void CreateSpreadSheet()        {            string fileName = "E:\\01.xlsx";            string sheetName = "test";            using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Create(fileName, SpreadsheetDocumentType.Workbook))            {                // create the workbook                  spreadSheet.AddWorkbookPart();                spreadSheet.WorkbookPart.Workbook = new Workbook();     // create the worksheet                  spreadSheet.WorkbookPart.AddNewPart
(); spreadSheet.WorkbookPart.WorksheetParts.First().Worksheet = new Worksheet(); SharedStringTablePart m_SharedStringTablePart = spreadSheet.WorkbookPart.AddNewPart
(); // create sheet data SheetData m_SheetData = new SheetData(); spreadSheet.WorkbookPart.WorksheetParts.First().Worksheet.AppendChild(m_SheetData); // create row //spreadSheet.WorkbookPart.WorksheetParts.First().Worksheet.First().AppendChild(new Row()); //// create cell with data //spreadSheet.WorkbookPart.WorksheetParts.First().Worksheet.First().First().AppendChild(new Cell() { CellValue = new CellValue("100") }); //Add Data Row row1 = new Row() { RowIndex = 1 }; Cell cell1 = new Cell() { CellReference = "A1" }; int index = InsertSharedStringItem("Sun", m_SharedStringTablePart); cell1.CellValue = new CellValue(index.ToString()); cell1.DataType = new EnumValue
(CellValues.SharedString); row1.Append(cell1); m_SheetData.Append(row1); // save worksheet spreadSheet.WorkbookPart.WorksheetParts.First().Worksheet.Save(); // create the worksheet to workbook relation spreadSheet.WorkbookPart.Workbook.AppendChild(new Sheets()); spreadSheet.WorkbookPart.Workbook.GetFirstChild
().AppendChild(new Sheet() { Id = spreadSheet.WorkbookPart.GetIdOfPart(spreadSheet.WorkbookPart.WorksheetParts.First()), SheetId = 1, Name = sheetName }); spreadSheet.WorkbookPart.Workbook.Save(); } } private static int InsertSharedStringItem(string text, SharedStringTablePart shareStringPart) { // If the part does not contain a SharedStringTable, create one. if (shareStringPart.SharedStringTable == null) { shareStringPart.SharedStringTable = new SharedStringTable(); shareStringPart.SharedStringTable.Count = 1; shareStringPart.SharedStringTable.UniqueCount = 1; } int i = 0; // Iterate through all the items in the SharedStringTable. If the text already exists, return its index. foreach (SharedStringItem item in shareStringPart.SharedStringTable.Elements
()) { if (item.InnerText == text) { return i; } i++; } // The text does not exist in the part. Create the SharedStringItem and return its index. shareStringPart.SharedStringTable.AppendChild(new SharedStringItem(new DocumentFormat.OpenXml.Spreadsheet.Text(text))); shareStringPart.SharedStringTable.Save(); return i; }

 

转载地址:http://enulx.baihongyu.com/

你可能感兴趣的文章
中国***安永烁
查看>>
WinAPI: ExtCreateRegion - 区域变换
查看>>
MySQLdb安装
查看>>
【Java每日一题】20170309
查看>>
Mysql数据库主从心得整理
查看>>
准备应对2014年变化中的APM市场
查看>>
我的友情链接
查看>>
IOS开发之UITableView1
查看>>
关于ARM的22个常用概念介绍
查看>>
Beetle.NetPackage加入Protobuf的支持(android,wp8)
查看>>
Exchange 2010安装配置
查看>>
经典语录
查看>>
Java学习笔记(29)——Java集合01之总体框架
查看>>
数据库权限设计
查看>>
.net 事件传递
查看>>
require和include区别
查看>>
新安装系统安装QQ不能使用
查看>>
react-navigation 导航栏使用
查看>>
vanish(squid) + HAProxy + nginx + memcached(redis)
查看>>
/etc/inittab文件详解
查看>>