从.csv文件转换为.xlsx excel文件
您好我正在使用一个ASP.NET应用程序,并生成Excel文件扩展名为.csv的报告。 但是我想用.xlsx etension生成它。 我正在使用的代码如下:受保护的小组btnSubmit_Click(BYVAL发件人作为对象,BYVAL e作为EventArgs)处理btnSubmit.Click
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
任何帮助将不胜感激。 谢谢。
看看OpenXML SDK:我明白我的例子不会转换.csv文件,但它会引导你在正确的方向。 http://msdn.microsoft.com/en-us/library/bb448854(office.14).aspx
我已经在asp.net中使用它来实时创buildxlsx文档,直接传输到Web客户端:
public static System.IO.MemoryStream ConvertToExcel(DataSet ds) { System.IO.MemoryStream stream = new System.IO.MemoryStream(); using (SpreadsheetDocument package = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook, true)) { package.AddWorkbookPart(); package.WorkbookPart.Workbook = new Workbook(); package.WorkbookPart.AddNewPart<WorksheetPart>(); if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) { DataTable tbl = ds.Tables[0]; SheetData xlSheetData = new SheetData(); foreach (DataRow row in tbl.Rows) { Row xlRow = new Row(); foreach (DataColumn col in tbl.Columns) { object cellData = row[col]; Cell xlCell = null; if (cellData != null) { xlCell = new Cell(new InlineString(new DocumentFormat.OpenXml.Spreadsheet.Text(cellData.ToString()))) { DataType = CellValues.InlineString }; } else { xlCell = new Cell(new InlineString(new DocumentFormat.OpenXml.Spreadsheet.Text(String.Empty))) { DataType = CellValues.InlineString }; } xlRow.Append(xlCell); } xlSheetData.Append(xlRow); } package.WorkbookPart.WorksheetParts.First().Worksheet = new Worksheet(xlSheetData); package.WorkbookPart.WorksheetParts.First().Worksheet.Save(); // create the worksheet to workbook relation package.WorkbookPart.Workbook.AppendChild(new Sheets()); package.WorkbookPart.Workbook.GetFirstChild<Sheets>().AppendChild(new Sheet() { Id = package.WorkbookPart.GetIdOfPart(package.WorkbookPart.WorksheetParts.First()), SheetId = 1, Name = "Sheet1" }); package.WorkbookPart.Workbook.Save(); } } return stream; }
从我阅读的内容(还没有尝试过)中,我相信最好的方法是设置你的HTTP响应头文件。
总之,关键似乎是将内容处置标题设置为“附件”。
例如:
Content-Disposition: attachment; filename=<file name.ext>
对不起,我没有testing过这个,但在我search的东西密切相关,几乎每个地方我build议使用这种方法来强制下载对话框。