如何输出一个Excel电子表格与ASP.net的图像

假设我正在编写一个ASP.net Web应用程序。

该应用程序的原始devise者使用“PopChart”使一些图像出现在Web应用程序中。 其中一个页面上有一个“导出到Excel”button。 单击button时,页面内容将输出到Excel,如本教程所示: http : //aspalliance.com/articleViewer.aspx? aId=1& pId= 。

基本上,它将HTML输出到一个XLS文件,Excel解释它。 问题是,我需要摆脱PopChart。 我已经用一个输出PNG文件的网页控件(它可以在运行时保存在一个MemoryStream对象中)replace它。 PopChart引用从公共服务器运行的Flash组件; “PopChart”中的数据embedded在HTML中。

但是,一旦我开始使用输出PNG的Web控件,所有的数据都在一个PNG文件中。 我看不出有什么办法用HTML输出PNG文件。 我尝试将数据embedded到img src=... ,但Excel不会解释它。 Excel也不能从服务器下载PNG文件。 服务器被locking,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(); } } } } 

此代码的function是创build一个embedded了图像的新Excel电子表格。 但是,我一直无法修改这段代码,将图像放在我想要的电子表格上。 (是的,我尝试更改偏移量的值。)我也没有成功地将任何文本或数字数据添加到行和列。

有没有人得到这个工作?

看看NPOI库,这是一个很好的库,用于在.Net中生成Excel(和其他Officetypes)文档。

在他们的库中,他们有一个示例演示如何将图像加载到从文件读取的Excel中。 既然你可以让你的POPChart可以把图像保存为一个内存stream,你可以很容易地拿他们的例子,修改它来从内存stream而不是文件中读取。 Excel生成过程的其余部分应该很容易。

要更改图片的位置,您需要更改rowId和columnId。 它们充当行和列索引,所以如果你有ColumnId cId = new ColumnId(“2”); 和RowId rId = new RowId(“1”); 你的图像将被放置在单元格C2。 如果您使用的是twoCellAnchor,则需要执行两次,一次是起始索引,一次是结束索引。