Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Experts,

As per the comments given by Richard Deeming. I tried to use EEplus for exporting data to Excel.
Here is my code
Dim file As FileInfo = New FileInfo("ClaimForms_")
           Using pck As New ExcelPackage(file)
               Dim ws As ExcelWorksheet = pck.Workbook.Worksheets.Add("Sheet3")
               Dim dsExcel As DataSet = dbnet.GetAllDetails()
               Dim dt As DataTable = dsExcel.Tables(0)

               ws.Cells("A1").LoadFromDataTable(dt, True)
               ws.Cells.AutoFitColumns()
               Using rng As ExcelRange = ws.Cells(1, 1, 1, dt.Columns.Count)
                   rng.Style.Font.Bold = True
                   rng.Style.Fill.PatternType = ExcelFillStyle.Solid
                   rng.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.FromArgb(79, 129, 189))
                   rng.Style.Font.Color.SetColor(System.Drawing.Color.White)
               End Using
               pck.Save()

               Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
               Dim fileName As String = String.Format("ClaimReport_{0:yyyyMMdd_HH_mm}.xlsx", DateTime.Now)
               Dim disposition As String = String.Format("attachment; filename=""{0}""", fileName)
               Response.AppendHeader("Content-Disposition", disposition)
               Response.BinaryWrite(pck.GetAsByteArray())
               Response.TransmitFile(file.Name)
               Response.Flush()
           End Using

I get errors at the below line
Response.BinaryWrite(pck.GetAsByteArray())
saying that Part already exists. Also when I try to open the Excel. It is blank empty showing that it is corrupted


could someone help me with this please?
Posted
Updated 10-Apr-15 3:51am
v3

Remove the pck.Save() line.

You'll also need to remove the Response.TransmitFile(file.Name) line - you've already sent the file on the previous line.
 
Share this answer
 
v2
Comments
sudevsu 10-Apr-15 11:04am    
I tried both didn't work. Still it saves file corrupted
sudevsu 10-Apr-15 11:32am    
Thanks Richard
With reference to Richard deeming solution 1. I finally got solution
Here is my Code:
Dim file As FileInfo = New FileInfo("ClaimForms.xls")
       Using pck As New ExcelPackage(file)
           Dim wsClaims As ExcelWorksheet = pck.Workbook.Worksheets.Add("Sheet1")
           wsClaims.TabColor = Color.Blue

           Dim wslineitems As ExcelWorksheet = pck.Workbook.Worksheets.Add("Sheet2")
           wslineitems.TabColor = Color.Blue

           Dim dsExcel As DataSet = dbnet.GetAllDetails()
           Dim dtClaims As DataTable = dsExcel.Tables(0)
           Dim dtLineItems As DataTable = dsExcel.Tables(1)

           wsClaims.Cells("A1").LoadFromDataTable(dtClaims, True)
           wslineitems.Cells("A1").LoadFromDataTable(dtLineItems, True)
           wsClaims.Cells.AutoFitColumns()
           wslineitems.Cells.AutoFitColumns()
           Using rng As ExcelRange = wsClaims.Cells(1, 1, 1, dtClaims.Columns.Count)
               rng.Style.Font.Bold = True
               rng.Style.Fill.PatternType = ExcelFillStyle.Solid
               rng.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Blue)
               rng.Style.Font.Color.SetColor(System.Drawing.Color.Yellow)
           End Using
           Using rng As ExcelRange = wslineitems.Cells(1, 1, 1, dtLineItems.Columns.Count)
               rng.Style.Font.Bold = True
               rng.Style.Fill.PatternType = ExcelFillStyle.Solid
               rng.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Blue)
               rng.Style.Font.Color.SetColor(System.Drawing.Color.Yellow)
           End Using
           Dim result As MemoryStream = New MemoryStream
           pck.SaveAs(result)
           Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
           Dim fileName As String = String.Format("ClaimReport_{0:yyyyMMdd_HH_mm}.xlsx", DateTime.Now)
           Dim disposition As String = String.Format("attachment; filename=""{0}""", fileName)
           Response.AppendHeader("Content-Disposition", disposition)
           result.WriteTo(Response.OutputStream)
           'Response.BinaryWrite(pck.GetAsByteArray())
           'Response.TransmitFile(file.Name)
           ' Response.Flush()
           Response.End()
       End Using


I deleted the Excelpackage.dll which I downloaded from EEplus website. I added new dll called EEplus.dll. Everything works perfectly well.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900