0

我正在尝试保存我的图表图像,以便可以将其放入使用 epplus 创建的 excel 文件中

Response.ContentType = "image/bmp"
    Response.AppendHeader("Content-Disposition", "attachment;filename=chart.bmp")
    Chart1.SaveImage(Response.OutputStream)

    Dim imgchart As Bitmap
    imgchart = New Bitmap("chart.bmp")

Using package As New ExcelPackage(newFile)
        ' add a new worksheet to the empty workbook
        Dim worksheet As ExcelWorksheet = package.Workbook.Worksheets.Add("LIMS")
        'Add the headers
        worksheet.Cells(1, 1).Value = "Analyzer Time"
        worksheet.Cells(1, 2).Value = "Analyzer Data"
        worksheet.Cells(1, 3).Value = "Lab Date"
        worksheet.Cells(1, 4).Value = "Lab Data"
        Dim row
        row = 2
        For i = 0 To count1 - 1
            worksheet.Cells(row, 1).Value = col1(i)
            worksheet.Cells(row, 2).Value = col2(i)
            row = row + 1
        Next
        row = 2
        For i = 0 To labcount - 1
            worksheet.Cells(row, 3).Value = col3(i)
            worksheet.Cells(row, 4).Value = col4(i)
            row = row + 1
        Next

        worksheet.Drawings.AddPicture("chart", imgchart)
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
        Response.AppendHeader("Content-Disposition", "attachment;filename=chart.xlsx")
        package.SaveAs(Response.OutputStream)

        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
        Response.AppendHeader("Content-Disposition", "attachment;filename=chart.xlsx")
        Response.WriteFile("chart.xlsx")
        Response.End()
    End Using

我需要在此代码中修复什么?我是文件流和响应的新手。

4

1 回答 1

1

乍一看,我认为您犯了几个错误。首先,您似乎正在尝试通过响应发送图像和 excel。我不相信您可以将多个文件发送给客户端,但无论如何您都不需要发送图像,因为您只是将其插入到 excel 文件中。此外,您尝试从不存在的服务器文件“chart.bmp”加载位图的图像。其次,您正在使用Response.WriteFile将磁盘文件写入响应的功能。我猜这不是您想要做的,因为您已经将文件写入响应中Package.SaveAs。试试下面的代码,

    'Save the chart to a memory stream instead of a file 
    Dim BitMapMS As New MemoryStream
    Chart1.SaveImage(BitMapMS)
    'Load bitmap from memory stream
    Dim imgchart As Bitmap
    imgchart = New Bitmap(BitMapMS)

Using package As New ExcelPackage(newFile)
        ' add a new worksheet to the empty workbook
        Dim worksheet As ExcelWorksheet = package.Workbook.Worksheets.Add("LIMS")
        'Add the headers
        worksheet.Cells(1, 1).Value = "Analyzer Time"
        worksheet.Cells(1, 2).Value = "Analyzer Data"
        worksheet.Cells(1, 3).Value = "Lab Date"
        worksheet.Cells(1, 4).Value = "Lab Data"
        Dim row
        row = 2
        For i = 0 To count1 - 1
            worksheet.Cells(row, 1).Value = col1(i)
            worksheet.Cells(row, 2).Value = col2(i)
            row = row + 1
        Next
        row = 2
        For i = 0 To labcount - 1
            worksheet.Cells(row, 3).Value = col3(i)
            worksheet.Cells(row, 4).Value = col4(i)
            row = row + 1
        Next

        worksheet.Drawings.AddPicture("chart", imgchart)

        'Clear the response
        Response.Clear()
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"            
        Response.AppendHeader("Content-Disposition", "attachment;filename=chart.xlsx")
        'This call looks like it will write the data to the stream.
        package.SaveAs(Response.OutputStream)


        Response.End()
    End Using

我无法测试上面的代码,所以试一试,让我们知道它是怎么回事。

于 2012-12-13T17:29:08.593 回答