Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 下载生成的OpenXML_C#_Asp.net_Excel_Openxml - Fatal编程技术网

C# 下载生成的OpenXML

C# 下载生成的OpenXML,c#,asp.net,excel,openxml,C#,Asp.net,Excel,Openxml,我目前正在控制台应用程序中创建一个Excel工作表,用于测试目的。我需要在ASP.Net.aspx页面中实现我的代码 如何向用户提供此Excel表格供其下载,而不在本地以任何形式保存 这是我的代码: static void Main(string[] args) { SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(DateTime.Now.ToString("ddMM

我目前正在控制台应用程序中创建一个Excel工作表,用于测试目的。我需要在ASP.Net.aspx页面中实现我的代码

如何向用户提供此Excel表格供其下载,而不在本地以任何形式保存

这是我的代码:

        static void Main(string[] args)
    {
        SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(DateTime.Now.ToString("ddMMyyyyHHmmss") + @".xlsx", SpreadsheetDocumentType.Workbook);

        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 = "mySheet"
        };

        sheets.Append(sheet);

        Cell cell = InsertCellInWorksheet("A", 1, worksheetPart);
        cell.CellValue = new CellValue("23.289374");
        cell.DataType = new EnumValue<CellValues>(CellValues.Number);

        Cell cellString = InsertCellInWorksheet("B", 1, worksheetPart);
        cellString.CellValue = new CellValue("hello");
        cellString.DataType = new EnumValue<CellValues>(CellValues.String);

        // Close the document.
        workbookpart.Workbook.Save();
        spreadsheetDocument.Close();
    }

    // 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, 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;
        }
    }
static void Main(字符串[]args)
{
SpreadsheetDocument SpreadsheetDocument=SpreadsheetDocument.Create(DateTime.Now.ToString(“ddmmyyyhhmmss”)+@.xlsx,“SpreadsheetDocumentType.工作簿”);
WorkbookPart WorkbookPart=电子表格文档.AddWorkbookPart();
workbookpart.工作簿=新工作簿();
//将工作表部件添加到工作簿部件。
WorksheetPart WorksheetPart=workbookpart.AddNewPart();
worksheetPart.Worksheet=新工作表(new SheetData());
//将工作表添加到工作簿中。
Sheets Sheets=spreadsheetDocument.WorkbookPart.Workbook.AppendChild(新工作表());
//附加新工作表并将其与工作簿关联。
图纸=新图纸()
{
Id=spreadsheetDocument.WorkbookPart。
GetIdOfPart(工作表部分),
SheetId=1,
Name=“mySheet”
};
附页(页);
Cell Cell=插入工作表(“A”,1,工作表部分);
cell.CellValue=新的CellValue(“23.289374”);
cell.DataType=新的枚举值(CellValues.Number);
Cell cellString=InsertCellInWorksheet(“B”,1,工作表部分);
cellString.CellValue=新的CellValue(“hello”);
cellString.DataType=新的枚举值(CellValues.String);
//关闭文档。
workbookpart.Workbook.Save();
电子表格文档。关闭();
}
//给定列名、行索引和工作表部件,将单元格插入工作表中。
//如果单元格已存在,则返回它。
专用静态单元格InsertCellInWorksheet(字符串列名称、uint行索引、工作表部件工作表部件)
{
工作表=工作表零件工作表;
SheetData SheetData=工作表.GetFirstChild();
字符串cellReference=列名称+行索引;
//如果工作表不包含具有指定行索引的行,请插入一行。
行行;
if(sheetData.Elements().Where(r=>r.RowIndex==RowIndex).Count()!=0)
{
row=sheetData.Elements()。其中(r=>r.RowIndex==RowIndex)。First();
}
其他的
{
行=新行(){RowIndex=RowIndex};
sheetData.Append(行);
}
//如果没有具有指定列名的单元格,请插入一个。
if(row.Elements().Where(c=>c.CellReference.Value==columnName+rowIndex).Count()>0)
{
返回row.Elements(),其中(c=>c.CellReference.Value==CellReference.First();
}
其他的
{
//单元格必须按照CellReference的顺序排列。确定在何处插入新单元格。
Cell refCell=null;
foreach(row.Elements()中的单元格)
{
if(string.Compare(cell.CellReference.Value,CellReference,true)>0)
{
refCell=单元格;
打破
}
}
Cell newCell=newCell(){CellReference=CellReference};
row.InsertBefore(newCell、refCell);
工作表。保存();
返回newCell;
}
}
使用一个。e、 g

然后,您需要在aspx页面的代码隐藏中执行以下操作:

using (MemoryStream mem = new CreateSpreadSheet(mem))
{
    Response.Clear();
    Response.ContentType = 
        @"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml";
    Response.AddHeader("content-disposition", "attachment;filename=name_your_file.xlsx");
    mem.WriteTo(Response.OutputStream);
    Response.End();
}
using (MemoryStream mem = new CreateSpreadSheet(mem))
{
    Response.Clear();
    Response.ContentType = 
        @"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml";
    Response.AddHeader("content-disposition", "attachment;filename=name_your_file.xlsx");
    mem.WriteTo(Response.OutputStream);
    Response.End();
}