/// <summary> /// 简单格式下载,只下载<paramref name="ds"/>的数据行 /// </summary> /// <param name="name">用户下载框中显示的保存文件名,例如:SN_08082600012.xls</param> /// <param name="prefix">内部生成的下载文件前缀,例如:SN</param> /// <param name="format">数据列的格式描述信息</param> /// <param name="ds"></param> /// <returns>返回下载文件的链接地址(使用download.aspx)</returns> public static string DownloadXls(string name, string prefix, IList <DownloadFormat> format, DataSet ds) { string fileName = prefix + DateTime.Now.ToString("_yyMMdd_HHmmss") + ".xls"; string filePath = DownloadFolder + fileName; ExcelApp excelapp = null; ExcelWorkbook excelBook = null; ExcelWorksheet excelSheet = null; try { excelapp = new ExcelApp(); excelapp.DisplayAlerts = false; excelBook = excelapp.NewWorkBook(); excelSheet = excelBook.Worksheets(1); int rowIndex = 1; for (int i = 0; i < format.Count; i++) { excelSheet.Cells(rowIndex, i + 1).Value = format[i].Title; } ExcelRange rg = excelSheet.Range(rowIndex, rowIndex, 1, format.Count); rg.SelectRange(); rg.Font.Bold = true; rg.HorizontalAlignment = 3; rg.Interior.SetColor(221, 221, 221); rowIndex++; #region 写文件 foreach (DataRow row in ds.Tables[0].Rows) { for (int i = 0; i < format.Count; i++) { DownloadFormat ft = format[i]; if (ft.ColumnIndex == null) { continue; } for (int j = 0; j < ft.ColumnIndex.Length; j++) { SetCellValue(excelSheet.Cells(rowIndex, i + 1), j, ft.Type, row, ft.ColumnIndex[j]); } } rowIndex++; } #endregion ExcelRange excelRange = excelSheet.Cells(); excelRange.SelectRange(); excelRange.AutoFit(); excelRange.Font.Size = 10; excelBook.SaveAs(filePath); } catch (Exception er) { throw er; } finally { if (excelSheet != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(excelSheet.COMObject); } if (excelBook != null) { excelBook.Close(); System.Runtime.InteropServices.Marshal.ReleaseComObject(excelBook.COMObject); } if (excelapp != null) { excelapp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(excelapp.COMObject); } } return("/download.aspx?type=p&name=" + Microsoft.JScript.GlobalObject.escape(name) + "&path=" + Microsoft.JScript.GlobalObject.escape(filePath)); }