zoukankan      html  css  js  c++  java
  • [OpenXml] Generate excel in memory and dump to file

    public static void GenerateExcelFromStream()
    {
        using (MemoryStream memoryStream = new MemoryStream()) {
            using (SpreadsheetDocument document = SpreadsheetDocument.Create(memoryStream, SpreadsheetDocumentType.Workbook)) {
                document.AddWorkbookPart();
                document.WorkbookPart.Workbook = new Workbook();
    
                document.WorkbookPart.AddNewPart<WorksheetPart>();
                document.WorkbookPart.WorksheetParts.First().Worksheet = new Worksheet();
                document.WorkbookPart.WorksheetParts.First().Worksheet.AppendChild(new SheetData());
    
                Row row = new Row();
                row.AppendChild<Cell>(new Cell() { CellValue = new CellValue("hahaasdfhkasddjjalsdkjflaksdjfladskjflj"), DataType = CellValues.String, CellReference = "A1" });
                document.WorkbookPart.WorksheetParts.First().Worksheet.First().AppendChild(row);
                document.WorkbookPart.WorksheetParts.First().Worksheet.Save();
    
                document.WorkbookPart.Workbook.AppendChild(new Sheets());
                document.WorkbookPart.Workbook.GetFirstChild<Sheets>().AppendChild(new Sheet() {
                    Id = document.WorkbookPart.GetIdOfPart(document.WorkbookPart.WorksheetParts.First()),
                    SheetId = 1,
                    Name = "sheet1"
                });
                document.WorkbookPart.Workbook.Save(); 
            }
    
            using (FileStream fileStream = File.Create("generateExcel.xlsx")) {
                memoryStream.WriteTo(fileStream);
            }
        }
    }
  • 相关阅读:
    NHibernate中session.update()及session.merge()的区别
    子序列 (All in All,UVa 10340)
    古老的密码 (Ancient Cipher,NEERC 2004,LA 3213)
    例题1 勇者斗恶龙 (The Dragon of Loowater,UVa 11292)
    HDU1869 六度分离
    B. T-primes
    PoJ 1595 PrimeCuts
    poj 3518 Prime Gap
    PKU1988磁铁
    求组合数
  • 原文地址:https://www.cnblogs.com/webglcn/p/4679899.html
Copyright © 2011-2022 走看看