嗨 我正在使用ASP.NET應用程序並在Excel頁面中以.csv擴展名生成報告。不過,我想用.xlsx etension生成它。 我目前正在使用的代碼如下: 保護小組btnSubmit_Click(BYVAL發件人爲對象,BYVALË作爲EventArgs的)把手btnSubmit.Click從.csv文件轉換爲.xlsx excel文件
Dim sql As String
Dim strLine As String = ""
Dim attachment As String = "attachment; filename=PTW.csv"
m_sBranch = ddlBranches.SelectedValue
m_sRegion = ddlAreas.SelectedValue
Dim cnn As SqlConnection = New SqlConnection("Server=XYZ;Database=abc;Trusted_Connection=yes;")
HttpContext.Current.Response.AddHeader("content-disposition", attachment)
HttpContext.Current.Response.ContentType = "text/csv"
cnn.Open()
sql = GetReportSql(m_sBranch, m_sRegion)
Dim cmd As SqlCommand = New SqlCommand(sql, cnn)
Dim dr As SqlDataReader
dr = cmd.ExecuteReader()
HttpContext.Current.Response.Write("PTW JOBS - EXPORTED ON " + DateTime.Now)
For i = 0 To dr.FieldCount - 1
strLine = strLine & dr.GetName(i).ToString & ","
Next
HttpContext.Current.Response.Write(strLine)
Dim sb As StringBuilder = New StringBuilder()
Dim temp As String = ""
While dr.Read()
For i = 0 To dr.FieldCount - 1
temp = temp & dr.GetValue(i)
temp = temp.Replace(",", " ")
sb.Append(temp & ",")
temp = ""
Next
sb.AppendLine()
strLine = ""
End While
HttpContext.Current.Response.Write(sb.ToString())
End Sub
任何幫助將高度讚賞。謝謝。
查看內容標題列表。有xslx的文檔類型。另一個選擇,因爲你似乎與Excel 2007或更高版本一起工作是看看openxml格式。自從我使用任何打開的XML之後已經過去了幾個月,但是一旦你開始使用,它就變得非常簡單,並且你將能夠生成正確的Excel 2007文檔。如果您不知道此更改爲.zip的xlsx文件擴展名,並且您將能夠打開包含組成xslx – Andrew 2011-02-25 14:09:29