我有这个导出功能,允许我在一个excel中将2个网格视图导出为2个独立的工作表。如何将我的导出格式设置为microsoft.office.interop.excel中的excel工作簿?
但我的问题是:
我怎么能有一个像往常一样下载一个常用的弹出窗口,当我出口按钮,点击提示用户打开,另存为,取消下载,而不是将其保存的到一个特定的位置(目前我在我的代码中做什么)?
如何设置代码以启用所有单元格的wraptext = true,并自动格式化列高和宽以固定所有文本,以便它不显示######日期为例如,当excel打开时,列宽太小。
保护无效EXPORT_BUTTON_Click(对象发件人,EventArgs的) { Microsoft.Office.Interop.Excel._Application应用=新Microsoft.Office.Interop.Excel.Application(); //在Excel应用程序中创建新的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);
}
}
嗨我收到此错误Microsoft Excel无法访问文件'C:\ Users \ testacc \ Documents \〜\ ExcelFiles \ DCA76310'。有几种可能的原因: ?文件名或路径不存在。在工作簿。 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); –
嗨Ramesh,我想感谢你的帮助。除了目前尚未解决的这个问题外,我还在另一个帖子中为我的导出函数提供了一个额外的重要问题 - http://stackoverflow.com/questions/33161193/how-do-i- export-ajaxcontroltookkit-tabpanel-contentswhich-consist-of-multiple-c我希望你能帮我解答,谢谢:) –
嗨对不起,因为我不知道有关将图表导出到excel的问题 –