如何格式化我的导出到excel工作簿在microsoft.office.interop.excel?
我有这个导出function,允许我在一个Excel中导出2个网格视图到2个分离的工作表中。
但是我的问题是:
-
当我点击导出button来提示用户打开,另存为,取消下载,而不是将其保存到特定位置(目前我在我的代码中正在做什么)时,如何能像平常下载一样有一个正常的popup窗口?
-
如何设置一个代码来为所有单元格启用wraptext = true,并自动格式化列的高度和宽度,以固定所有文本,以便在列宽不变时显示######date作为示例excel打开的时候太小了。
protected void EXPORT_BUTTON_Click(object sender,EventArgs e){Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application(); //在Excel应用程序中创build新的WorkBook Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
String DT1 = "Data table 1"; String DT2 = "Data table 2"; ExportToExcel(app, workbook, Gridview1, DT1, 1); ExportToExcel(app, workbook, Gridview2, DT2, 2); } public void ExportToExcel(Microsoft.Office.Interop.Excel._Application app, Microsoft.Office.Interop.Excel._Workbook workbook, GridView gridview, string SheetName, int sheetid) { // see the excel sheet behind the program app.Visible = true; // get the reference of first sheet. By default its name is Sheet1. // store its reference to worksheet worksheet = (Excel.Worksheet)workbook,Worksheets.Add(); // changing the name of active sheet worksheet.Name = SheetName; // storing header part in Excel for (int i = 1; i < gridview.Columns.Count + 1; i++) { worksheet.Cells[1, i] = gridview.Columns[i - 1].HeaderText; } // storing Each row and column value to excel sheet for (int i = 0; i < gridview.Rows.Count - 1; i++) { for (int j = 0; j < gridview.Columns.Count; j++) { worksheet.Cells[i + 2, j + 1] = gridview.Rows[i].Cells[j].Text.ToString(); } } //save the application workbook.SaveAs(@"C:\Users\test\Desktop\Test\" + datetime.ToString("dd-MM-yyyy_hh-mm-ss") + ".xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing); } }
修改excel文件的path以保存到一个虚拟path,如下所示
workbook.SaveAs(@"C:\Users\test\Desktop\Test\" + datetime.ToString("dd-MM-yyyy_hh-mm-ss") + ".xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
改变这个
workbook.SaveAs(@"~/ExcelFiles/Filename.xlsx" + datetime.ToString("dd-MM-yyyy_hh-mm-ss") + ".xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
尝试以下代码显示另存为对话框
String FileName = "FileName.xlsx"; String FilePath = "~/ExcelFiles/FileName.xlsx"; System.Web.HttpResponse response = System.Web.HttpContext.Current.Response; response.ClearContent(); response.Clear(); response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";"); response.TransmitFile(FilePath); response.Flush(); response.End();
尝试这个:
Excel.Worksheet worksheet =(Excel.Worksheet)workbook.Worksheets["Sheet" + sheetid];
如Laxmikant指定的那样修改您的方法“ExportToExcel”的代码,如下所示。
public void ExportToExcel(Microsoft.Office.Interop.Excel._Application app, Microsoft.Office.Interop.Excel._Workbook workbook, GridView gridview, string SheetName, int sheetid) { // see the excel sheet behind the program app.Visible = true; worksheet = (Excel.Worksheet)workbook.Worksheets.Add(); // changing the name of active sheet worksheet.Name = SheetName; // storing header part in Excel for (int i = 1; i < gridview.Columns.Count + 1; i++) { worksheet.Cells[1, i] = gridview.Columns[i - 1].HeaderText; } // storing Each row and column value to excel sheet for (int i = 0; i < gridview.Rows.Count - 1; i++) { for (int j = 0; j < gridview.Columns.Count; j++) { worksheet.Cells[i + 2, j + 1] = gridview.Rows[i].Cells[j].Text.ToString(); } }
我删除了这两行代码,现在不需要参数“Sheetid”
Excel.Worksheet worksheet =(Excel.Worksheet)workbook.Worksheets["Sheet" + sheetid]; worksheet = workbook.ActiveSheet;
希望这将解决您的问题
您尚未在工作簿中添加工作表
尝试下面的代码
worksheet =(Excel._Worksheet)oXL.Worksheets.Add();
worksheet.Name = SheetName;
更多信息查看http://dotnetmentors.com/c-sharp/how-to-export-sql-data-to-excel-using-microsoft-office-interop.aspx
使用范围设置您的标题
string[] colNames = new string[gv.Columns.Count]; int col = 0; foreach(gvvolumns dc in GridView.Columns) colNames[col++] = dc.ColumnName; char lastColumn = (char)(65 + dtProducts.Columns.Count - 1); oSheet.get_Range("A1", lastColumn + "1").Value2 = colNames; oSheet.get_Range("A1", lastColumn + "1").Font.Bold = true; oSheet.get_Range("A1", lastColumn + "1").VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;