我写了一个ashx来构建一个excel文件,从我的数据库中导出一些数据。 当我打开文件时,我的消息说:您尝试打开的文件格式与指定的文件扩展名不同...
这是我的代码:
Imports System.Web
导入System.Web.Services Imports System.IO
公共类DowloadClient 实现System.Web.IHttpHandler
Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
'set header
context.Response.Clear()
context.Response.AddHeader("content-disposition", "attachment; filename=Clients.xls")
context.Response.ContentType = "application/ms-excel"
context.Response.ContentEncoding = Encoding.UTF8
Dim exportContent As String = ExportToExcel()
context.Response.Write(exportContent)
'write the file
context.Response.End()
End Sub
Private Function ExportToExcel() As String
Dim _exportContent As String = ""
Dim q = GetClientsCRM(New NatCallEntities)
Using sb As New IO.StringWriter()
Using htmlWriter As New HtmlTextWriter(sb)
Dim table As New WebControls.Table()
table.GridLines = GridLines.Horizontal
table.BorderWidth = New Unit(1)
Dim header As New TableRow()
header.BackColor = Drawing.Color.Gray
Dim h As New TableCell()
h.BorderWidth = New Unit(1)
h.Text = "Nom"
header.Cells.Add(h)
Dim h1 As New TableCell()
h1.BorderWidth = New Unit(1)
h1.Text = "PreNom"
header.Cells.Add(h1)
Dim h2 As New TableCell()
h2.BorderWidth = New Unit(1)
h2.Text = "Conjoint"
header.Cells.Add(h2)
Dim h3 As New TableCell()
h3.BorderWidth = New Unit(1)
h3.Text = "Proprietaire Adresse"
header.Cells.Add(h3)
Dim h4 As New TableCell()
h4.BorderWidth = New Unit(1)
h4.Text = "Mention Adresse"
header.Cells.Add(h4)
Dim h5 As New TableCell()
h5.BorderWidth = New Unit(1)
h5.Text = "Adresse"
header.Cells.Add(h5)
Dim h6 As New TableCell()
h6.BorderWidth = New Unit(1)
h6.Text = "Complement Adresse"
header.Cells.Add(h6)
Dim h7 As New TableCell()
h7.BorderWidth = New Unit(1)
h7.Text = "Code Postale"
header.Cells.Add(h7)
Dim h8 As New TableCell()
h8.BorderWidth = New Unit(1)
h8.Text = "Ville"
header.Cells.Add(h8)
Dim h9 As New TableCell()
h9.BorderWidth = New Unit(1)
h9.Text = "Telephone"
header.Cells.Add(h9)
Dim h10 As New TableCell()
h10.BorderWidth = New Unit(1)
h10.Text = "Representant"
header.Cells.Add(h10)
table.Rows.Add(header)
If q IsNot Nothing AndAlso q.Count > 0 Then
Dim i As Integer = 0
For Each clt In q
Dim row As New TableRow()
Dim cell As New TableCell()
cell.BorderWidth = New Unit(1)
cell.Text = HttpUtility.HtmlEncode(clt.Nom)
row.Cells.Add(cell)
Dim cell1 As New TableCell()
cell1.BorderWidth = New Unit(1)
cell1.Text = HttpUtility.HtmlEncode(clt.Prenom)
row.Cells.Add(cell1)
Dim cell2 As New TableCell()
cell2.BorderWidth = New Unit(1)
cell2.Text = HttpUtility.HtmlEncode(clt.Conjoint)
row.Cells.Add(cell2)
Dim cell3 As New TableCell()
cell3.BorderWidth = New Unit(1)
cell3.Text = HttpUtility.HtmlEncode(clt.Proprietaire)
row.Cells.Add(cell3)
Dim cell4 As New TableCell()
cell4.BorderWidth = New Unit(1)
cell4.Text = HttpUtility.HtmlEncode(clt.Mention)
row.Cells.Add(cell4)
Dim cell5 As New TableCell()
cell5.BorderWidth = New Unit(1)
cell5.Text = HttpUtility.HtmlEncode(clt.Adresse)
row.Cells.Add(cell5)
Dim cell6 As New TableCell()
cell6.BorderWidth = New Unit(1)
cell6.Text = HttpUtility.HtmlEncode(clt.Complement)
row.Cells.Add(cell6)
Dim cell7 As New TableCell()
cell7.BorderWidth = New Unit(1)
cell7.Text = clt.CP
row.Cells.Add(cell7)
Dim cell8 As New TableCell()
cell8.BorderWidth = New Unit(1)
cell8.Text = HttpUtility.HtmlEncode(clt.Ville)
row.Cells.Add(cell8)
Dim cell9 As New TableCell()
cell9.BorderWidth = New Unit(1)
cell9.Text = clt.Tel
row.Cells.Add(cell9)
Dim cell10 As New TableCell()
cell10.BorderWidth = New Unit(1)
cell10.Text = HttpUtility.HtmlEncode(clt.Representant)
row.Cells.Add(cell10)
table.Rows.Add(row)
Next
End If
table.RenderControl(htmlWriter)
_exportContent = sb.ToString()
End Using
End Using
Return _exportContent
End Function
ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class
当我打开文件时,一切都很好。 如果没有这条消息我该怎么办?
答案 0 :(得分:0)
最后我找到了一个好的图书馆:EPPLUS