zoukankan      html  css  js  c++  java
  • Microsoft SDK for Open XML Formats Technology Preview 发布

    下载地址:
    http://go.microsoft.com/?linkid=6899996

    帮助文档里有一个创建docx的例子,下面提供一个创建xlsx的例子

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Office.DocumentFormat.OpenXml.Packaging;
    using System.IO;
    using System.Xml;


    namespace ConsoleApplication1
    {
        
    class Program
        
    {
            
    static void Main(string[] args)
            
    {           
                CreateNewXlsDocument(
    "c:\\temp.xlsx");
            }

            

            
    public static void CreateNewXlsDocument(string document)
            
    {         
                
    using (SpreadsheetDocument doc = SpreadsheetDocument.Create(document, SpreadsheetDocumentType.Workbook))
                
    {              

                    WorkbookPart mainPart 
    = doc.AddWorkbookPart();
                    WorksheetPart part 
    = mainPart.AddNewPart<WorksheetPart>();

                    
    string rid = mainPart.GetIdOfPart(part);

                    SetWorkBookContect(mainPart,rid);
                    SetWorkSheetContect(part);
                }

            }


            
    public static void SetWorkBookContect(WorkbookPart part,string rid)
            
    {
                
    const string xlsXml = @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?><workbook xmlns=""http://schemas.openxmlformats.org/spreadsheetml/2006/main"" xmlns:r=""http://schemas.openxmlformats.org/officeDocument/2006/relationships""><sheets><sheet name=""Sheet1"" sheetId=""1"" r:id=""{0}""/></sheets></workbook>";
                
    using (Stream stream = part.GetStream())
                
    {
                    
    byte[] buf = (new UTF8Encoding()).GetBytes(string.Format(xlsXml,rid));
                    stream.Write(buf, 
    0, buf.Length);
                }

            }



            
    public static void SetWorkSheetContect(WorksheetPart part)
            
    {
                
    const string xlsXml =
             
    @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>
    <worksheet xmlns=""http://schemas.openxmlformats.org/spreadsheetml/2006/main"" xmlns:r=""http://schemas.openxmlformats.org/officeDocument/2006/relationships"">
      <sheetData>
        <row>
          <c t=""inlineStr"">
            <is>
              <t>测试</t>
            </is>
          </c>      
        </row>    
      </sheetData>
    </worksheet>
    ";
                           
                
    using (Stream stream = part.GetStream())
                
    {
                    
    byte[] buf = (new UTF8Encoding()).GetBytes(xlsXml);
                    stream.Write(buf, 
    0, buf.Length);
                }

            }


        }

    }

  • 相关阅读:
    VPS CenteOS Linux 上传 下载文件(Apache配置、SSH)
    tar命令加密压缩
    操作系统命令技巧备忘录
    网络流量分析-PCAP切割、筛选、合并
    【Shell】30分钟关闭Tcpdump,开启Tcpdump、检测目录大小终止任务
    大数据做安全的网站
    WinRAR代码执行漏洞CVE-2018-20250
    Linux嗅探ettercap
    WindowsPE权威指南-PE文件头中的重定位表
    推荐书籍-恶意软件分析诀窍与工具箱
  • 原文地址:https://www.cnblogs.com/derek/p/773932.html
Copyright © 2011-2022 走看看