2010-10-26 67 views
2

假設我正在編寫一個ASP.net Web應用程序。如何使用ASP.net輸出Excel電子表格

該應用程序的原始設計者使用「PopChart」使某些圖像顯示在Web應用程序中。其中一個頁面上有一個「導出到Excel」按鈕。點擊按鈕後,頁面內容將輸出到Excel中,如本教程所示:http://aspalliance.com/articleViewer.aspx?aId=1&pId=

基本上,它將HTML輸出到XLS文件並由Excel解釋它。問題是,我需要擺脫PopChart。我用一個輸出PNG文件的網頁控件替換它(這可以在運行時保存在MemoryStream對象中)。 PopChart引用從公共服務器運行的Flash組件; 「PopChart」中的數據嵌入在HTML中。

但是,一旦我開始使用輸出PNG的網頁控件,所有的數據都在一個PNG文件中。我看不出有什麼辦法可以用HTML輸出PNG文件。我試圖將數據嵌入img src=...,但Excel不會解釋它。 Excel也不能從服務器下載PNG文件;服務器被鎖定,Excel無法訪問瀏覽器會話。

我該怎麼辦?

更新:我發現在MSDN論壇上給出了下面的代碼示例:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

using DocumentFormat.OpenXml; 
using DocumentFormat.OpenXml.Packaging; 
using DocumentFormat.OpenXml.Spreadsheet; 
using DocumentFormat.OpenXml.Validation; 
using DocumentFormat.OpenXml.Drawing.Spreadsheet; 

using System.IO; 
using System.Drawing; 

namespace ConsoleApplication4 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string sFile = "ExcelOpenXmlWithImage.xlsx"; 
      if (File.Exists(sFile)) 
      { 
       File.Delete(sFile); 
      } 
      BuildWorkbook(sFile); 
     } 

     private static void BuildWorkbook(string filename) 
     { 
      try 
      { 
       using (SpreadsheetDocument xl = SpreadsheetDocument.Create(filename, SpreadsheetDocumentType.Workbook)) 
       { 
        WorkbookPart wbp = xl.AddWorkbookPart(); 
        WorksheetPart wsp = wbp.AddNewPart<WorksheetPart>();         
        Workbook wb = new Workbook(); 
        FileVersion fv = new FileVersion(); 
        fv.ApplicationName = "Microsoft Office Excel"; 
        Worksheet ws = new Worksheet(); 
        SheetData sd = new SheetData(); 

        //string sImagePath = "polymathlogo.png"; 
        string sImagePath = @"c:\temp\chartImg.png"; 
        DrawingsPart dp = wsp.AddNewPart<DrawingsPart>(); 
        ImagePart imgp = dp.AddImagePart(ImagePartType.Png, wsp.GetIdOfPart(dp)); 
        using (FileStream fs = new FileStream(sImagePath, FileMode.Open)) 
        { 
         imgp.FeedData(fs); 
        } 

        NonVisualDrawingProperties nvdp = new NonVisualDrawingProperties(); 
        nvdp.Id = 1025; 
        nvdp.Name = "Picture 1"; 
        nvdp.Description = "polymathlogo"; 
        DocumentFormat.OpenXml.Drawing.PictureLocks picLocks = new DocumentFormat.OpenXml.Drawing.PictureLocks(); 
        picLocks.NoChangeAspect = true; 
        picLocks.NoChangeArrowheads = true; 
        NonVisualPictureDrawingProperties nvpdp = new NonVisualPictureDrawingProperties(); 
        nvpdp.PictureLocks = picLocks; 
        NonVisualPictureProperties nvpp = new NonVisualPictureProperties(); 
        nvpp.NonVisualDrawingProperties = nvdp; 
        nvpp.NonVisualPictureDrawingProperties = nvpdp; 

        DocumentFormat.OpenXml.Drawing.Stretch stretch = new DocumentFormat.OpenXml.Drawing.Stretch(); 
        stretch.FillRectangle = new DocumentFormat.OpenXml.Drawing.FillRectangle();      

        BlipFill blipFill = new BlipFill(); 
        DocumentFormat.OpenXml.Drawing.Blip blip = new DocumentFormat.OpenXml.Drawing.Blip(); 
        blip.Embed = dp.GetIdOfPart(imgp); 
        blip.CompressionState = DocumentFormat.OpenXml.Drawing.BlipCompressionValues.Print; 
        blipFill.Blip = blip; 
        blipFill.SourceRectangle = new DocumentFormat.OpenXml.Drawing.SourceRectangle(); 
        blipFill.Append(stretch); 

        DocumentFormat.OpenXml.Drawing.Transform2D t2d = new DocumentFormat.OpenXml.Drawing.Transform2D(); 
        DocumentFormat.OpenXml.Drawing.Offset offset = new DocumentFormat.OpenXml.Drawing.Offset(); 
        offset.X = 1000; 
        offset.Y = 1000; 
        t2d.Offset = offset; 
        Bitmap bm = new Bitmap(sImagePath); 
        //http://en.wikipedia.org/wiki/English_Metric_Unit#DrawingML 
        //http://stackoverflow.com/questions/1341930/pixel-to-centimeter 
        //http://stackoverflow.com/questions/139655/how-to-convert-pixels-to-points-px-to-pt-in-net-c 
        DocumentFormat.OpenXml.Drawing.Extents extents = new DocumentFormat.OpenXml.Drawing.Extents(); 
        extents.Cx = (long)bm.Width * (long)((float)914400/bm.HorizontalResolution); 
        extents.Cy = (long)bm.Height * (long)((float)914400/bm.VerticalResolution); 
        bm.Dispose(); 
        t2d.Extents = extents; 
        ShapeProperties sp = new ShapeProperties(); 
        sp.BlackWhiteMode = DocumentFormat.OpenXml.Drawing.BlackWhiteModeValues.Auto; 
        sp.Transform2D = t2d; 
        DocumentFormat.OpenXml.Drawing.PresetGeometry prstGeom = new DocumentFormat.OpenXml.Drawing.PresetGeometry(); 
        prstGeom.Preset = DocumentFormat.OpenXml.Drawing.ShapeTypeValues.Rectangle; 
        prstGeom.AdjustValueList = new DocumentFormat.OpenXml.Drawing.AdjustValueList(); 
        sp.Append(prstGeom); 
        sp.Append(new DocumentFormat.OpenXml.Drawing.NoFill()); 

        DocumentFormat.OpenXml.Drawing.Spreadsheet.Picture picture = new DocumentFormat.OpenXml.Drawing.Spreadsheet.Picture(); 
        picture.NonVisualPictureProperties = nvpp; 
        picture.BlipFill = blipFill; 
        picture.ShapeProperties = sp; 

        Position pos = new Position(); 
        pos.X =(Int64) 1000; 
        pos.Y =(Int64) 1000; 
        Extent ext = new Extent(); 
        ext.Cx = extents.Cx; 
        ext.Cy = extents.Cy; 
        AbsoluteAnchor anchor = new AbsoluteAnchor(); 
        anchor.Position = pos; 
        anchor.Extent = ext; 
        anchor.Append(picture); 
        anchor.Append(new ClientData()); 
        WorksheetDrawing wsd = new WorksheetDrawing(); 
        wsd.Append(anchor); 
        Drawing drawing = new Drawing(); 
        drawing.Id = dp.GetIdOfPart(imgp); 

        wsd.Save(dp);      

        ws.Append(sd); 
        ws.Append(drawing); 
        wsp.Worksheet = ws; 
        wsp.Worksheet.Save(); 
        Sheets sheets = new Sheets(); 
        Sheet sheet = new Sheet(); 
        sheet.Name = "Sheet1"; 
        sheet.SheetId = 1; 
        sheet.Id = wbp.GetIdOfPart(wsp); 
        sheets.Append(sheet); 
        wb.Append(fv); 
        wb.Append(sheets); 

        xl.WorkbookPart.Workbook = wb; 
        xl.WorkbookPart.Workbook.Save(); 
        xl.Close(); 
       } 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine(e.ToString()); 
       Console.ReadLine(); 
      } 
     } 


    } 
} 

此代碼函數來創建內嵌有圖像的新的Excel電子表格。但是,我一直無法修改此代碼以將圖像放在我想要的電子表格上。 (是的,我嘗試更改偏移量的值。)我也沒有成功將任何文本或數字數據添加到行和列中。

這裏有人得到這個工作?

+1

注意:我可以訪問DocumentFormat.OpenXml命名空間,但我不知道如何使用它,並且文檔很混亂。 – 2010-10-26 15:22:39

回答

1

查看NPOI庫,該庫是一個用於在.Net中生成Excel(和其他Office類型)文檔的出色庫。

在他們的庫中,他們有一個示例演示如何將圖像加載到從文件讀取的Excel中。由於您可以讓POPChart將圖像保存爲內存流,因此您可以輕鬆地將其示例修改爲從內存流而不是文件讀取。 Excel生成過程的其餘部分應該很簡單。

+1

如果它能幫助你擺脫困境,我最近寫了一篇關於使用NPOI將簡單的GridViews導出到Excel的博客文章。你可以在這裏找到:http://dillieodigital.wordpress.com/2010/10/19/soup-to-nuts-exporting-a-gridview-to-excel/ – 2010-10-26 15:32:51

+0

對不起,我在一家組織工作,有一個「已批准」庫的清單,它在CodePlex庫上非常短:-( – 2010-10-26 16:48:32

0

要更改圖片的位置,您需要更改rowId和columnId。它們充當行和列索引,所以如果你有ColumnId cId = new ColumnId(「2」);和RowId rId = new RowId(「1」);你的圖像將被放置在C2單元格中。如果您使用的是twoCellAnchor,則需要執行兩次,一次用於起始索引,一次用於結束索引。

相關問題