asp.net打开xml下载.xlsx文件

时间:2014-05-27 08:46:12

标签: asp.net openxml

我试图从网格中提供.xlsx文件,我认为大部分的努力都已完成。我正在拿起一个模板文件并用数据填充它。

我'收到错误'找不到文件'在Response.WriteFile上。

通过示例的外观(下面链接),这应该只是目标文件名,但我想这需要是文件的路径?所以我需要保存我的' myDoc&#39 ;对象到服务器,然后在Reponse.WriteFile中提供路径。

这似乎不是示例的含义。

我使用的代码是:http://technet.weblineindia.com/web/export-data-to-excel-using-openxml-sdk/

的修改版本

由于使用sharepoint来存储模板文件,我只需要创建一个文件流,而不是提供文件的URL。

这是我的代码:

        // Create cell reference array 
       string[] CellReferenceArray = new string[] { "A", "B", "C", "D", "E" };
        //Open your saved excel file that you have created using template file.
       using (SpreadsheetDocument myDoc = SpreadsheetDocument.Open(file.OpenBinaryStream(), true))
        {
            // Create reference of main Workbook part, which contains all reference.
            WorkbookPart objworkbook = myDoc.WorkbookPart;

            // Create style sheet object that will be used for applying styling.
            Stylesheet objstyleSheet = objworkbook.WorkbookStylesPart.Stylesheet;

            // pick up first worksheet
            WorksheetPart objworksheet = objworkbook.WorksheetParts.First();

            // will be used in end while creating sheet data
            string objorigninalSheetId = objworkbook.GetIdOfPart(objworksheet);
            WorksheetPart objreplacementPart = objworkbook.AddNewPart<WorksheetPart>();
            string objreplacementPartId = objworkbook.GetIdOfPart(objreplacementPart);

            // Create object reader to read from excel file.
            OpenXmlReader objreader = OpenXmlReader.Create(objworksheet);

            // create writer object to write in excel sheet.
            OpenXmlWriter objOpenXmwriter = OpenXmlWriter.Create(objreplacementPart);

            int i = 1;
            Row r = new Row();
            Cell c = new Cell();
            Columns col1 = new Columns();
            UInt32 index = 0;
            while (objreader.Read())
            {
                if (objreader.ElementType == typeof(SheetData))
                {
                    if (objreader.IsEndElement)
                        continue;

                    objOpenXmwriter.WriteStartElement(new SheetData());
                    objOpenXmwriter.WriteStartElement(r);

                    // Loop to insert header
                    foreach (DataColumn colHead in YoutdTName.Columns)
                    {
                        c = new Cell
                        {
                            DataType = CellValues.String,
                            CellReference = CellReferenceArray[i] + Convert.ToString(index)
                        };
                        CellValue v1 = new CellValue(colHead.ColumnName.ToString());
                        c.Append(v1);
                        objOpenXmwriter.WriteElement(c);
                        i += 1;
                    }
                    objOpenXmwriter.WriteEndElement();
                    index += 1;

                    //Loop to insert datatable row in excel 
                    foreach (DataRow dr in YoutdTName.Rows)
                    {
                        objOpenXmwriter.WriteStartElement(r);
                        i = 1;
                        foreach (DataColumn col in YoutdTName.Columns)
                        {
                            c = new Cell
                            {
                                DataType = CellValues.String,
                                CellReference = CellReferenceArray[i] + Convert.ToString(index)
                            };
                            CellValue v1 = new CellValue(dr[col].ToString());
                            c.AppendChild(v1);
                            objOpenXmwriter.WriteElement(c);
                            i += 1;
                        }
                        objOpenXmwriter.WriteEndElement();
                        index += 1;
                    }
                    objOpenXmwriter.WriteEndElement();
                }
            }
            //close all objects
            objreader.Close();
            objOpenXmwriter.Close();

            Sheet sheet = objworkbook.Workbook.Descendants<Sheet>().Where(s => s.Id.Value.Equals(objorigninalSheetId)).First();
            sheet.Id.Value = objreplacementPartId;
            objworkbook.DeletePart(objworksheet);


        }
       Response.AddHeader("Content-Disposition", "inline;filename=YourExcelfileName.xlxs");
       Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
       Response.WriteFile("YourExcelfileName.xlxs");
       Response.Flush();
       Response.End();

    }

1 个答案:

答案 0 :(得分:0)

改为使用HttpResponse.BinaryWrite并从SpreadsheetDocument实例中获取基础流。

http://msdn.microsoft.com/en-us/library/system.web.httpresponse.binarywrite(v=vs.110).aspx