如何添加在保存对话功能在我导出到Excel codeS? [英] How do I add in an save dialogue function in my export to excel codes?
本文介绍了如何添加在保存对话功能在我导出到Excel codeS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我会救了我的excel文件在使用2种方法,第1次为直接在服务器上保存,第二出口拿出一个保存对话框,让这样的用户选择适合自己的位置,在这种情况下2个文件将被保存在总在每一个我可以直接保存到服务器中,但出口的我如何添加与保存在我出口的对话保存功能脱颖而出codeS?
保护无效EXPORT_BUTTON_Click(对象发件人,EventArgs的发送)
{
Microsoft.Office.Interop.Excel._Application应用=新Microsoft.Office.Interop.Excel.Application(); //创建Excel应用程序中的新的工作簿
Microsoft.Office.Interop.Excel._Workbook工作簿= app.Workbooks.Add(Type.Missing); 串DT1 =数据表1;
字符串DT2 =数据表2; ExportToExcel(应用程序,工作簿,Gridview1,DT1,1); ExportToExcel(应用程序,工作簿,Gridview2,DT2,2); workbook.SaveAs(@C:\\用户\\ testacc \\桌面\\测试\\+则DateTime.ToString(DD-MM-yyyy_hh-MM-SS)+的.xlsx,Type.Missing,Type.Missing,类型.Missing,Type.Missing,Type.Missing,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
workbook.SaveAs(@〜/ ExcelFiles / Filename.xlsx,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,Type.Missing ,Type.Missing,Type.Missing,Type.Missing);
字符串文件名=FileName.xlsx;
字符串的文件路径=〜/ ExcelFiles / FileName.xlsx
System.Web.Htt presponse响应= System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType =应用程序/ vnd.openxmlformats-officedocument.s preadsheetml.sheet
response.AddHeader(内容处置,附件;文件名=+文件名+;);
response.TransmitFile(文件路径);
response.Flush();
到Response.End();
app.Quit();
}
公共无效ExportToExcel(Microsoft.Office.Interop.Excel._Application应用程序,Microsoft.Office.Interop.Excel._Workbook工作簿,GridView控件GridView控件,串SHEETNAME)
{
//看到程序后面的excel表
app.Visible = FALSE; Excel.Worksheet工作表=(Excel.Worksheet)workbook.Worksheets.Add(); //改变活动工作表的名称
worksheet.Name = SHEETNAME; //存储头部分在Excel中
的for(int i = 1; I< gridview.Columns.Count + 1;我++)
{
worksheet.Cells [1,I] = gridview.Columns [我 - 1] .HeaderText;
} //存储每一行和每一列值的Excel工作表
的for(int i = 0; I< gridview.Rows.Count - 1;我++)
{
对于(INT J = 0; J< gridview.Columns.Count; J ++)
{
worksheet.Cells [I + 2,J + 1] = gridview.Rows [I] .Cells [J] .Text.ToString();
}
}
}
解决方案
使用以下codeS问题解决:
Response.ContentType =应用程序/ vnd.openxmlformats-officedocument.s preadsheetml.sheet
Response.AddHeader(内容处置,附件;文件名= products.xlsx);
excel.SaveAs(MemoryStream的);
memoryStream.WriteTo(Response.OutputStream);
Response.Flush();
到Response.End();
I will be saving my excel file upon export using 2 methods, 1st to save as directly in server, 2nd to come up with a save dialogue to let user choose their own location thus in this case 2 files will be saved in total in each export which I am able to save directly into the server but How do I add in a save function with save dialogue in my export to excel codes?
protected void EXPORT_BUTTON_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
// creating new WorkBook within Excel application
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);
workbook.SaveAs(@"C:\Users\testacc\Desktop\Test\" + datetime.ToString("dd-MM-yyyy_hh-mm-ss") + ".xlsx", 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", 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();
app.Quit();
}
public void ExportToExcel(Microsoft.Office.Interop.Excel._Application app, Microsoft.Office.Interop.Excel._Workbook workbook, GridView gridview, string SheetName)
{
// see the excel sheet behind the program
app.Visible = false;
Excel.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();
}
}
}
解决方案
Question solved using the following codes:
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=products.xlsx");
excel.SaveAs(memoryStream);
memoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
这篇关于如何添加在保存对话功能在我导出到Excel codeS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文