【发布时间】:2014-11-22 14:10:38
【问题描述】:
我们有一些时间来下载 excel 点击按钮,而在中间我们想显示进度条
谁能帮帮我
我们在按钮单击和导出 excel 代码时有以下代码
protected void ibtnExport_Click(object sender, ImageClickEventArgs e) { 尝试 { 数据集 ds = new DataSet();
if (TabContainer1.ActiveTabIndex == 0)
{
ds = (DataSet)ViewState["DSCurrResponseSummary"];
if (ds != null && ds.Tables.Count > 0 && ds.Tables[2].Rows.Count > 0)
{
string ColName = "DOMAIN,PERIOD,CIRCLE,DEPARTMENT,SUB-DEPARTMENT,UNIT,ASSESSMENT NUMBER,ASSESSMENTNAME,TOTAL,PENDING,RESPONDED CONTROL,COMPLIANT,NON COMPLIANT,NOT APPLICABLE,TOTAL RESPONDENTS,STATUS,TARGET CLOSURE DATE,REVIEWED";
string strBindCols = "DOMAIN_NAME,PERIOD_NAME,CIRCLE_NAME,DEPARTMENT_NAME,SUBDEPARTMENT_NAME,UNIT_NAME,ASSESMENT_NO,ASSESMENT_NAME,TOTAL_RESPONSE,PENDING_RESPONSE,RESPONDED_CONTROL,COMPLIANT,NON_COMPLIANT,Non Applicable,Total Respondant,STATUS,RESPONSE_DUE_DATE,IS_REVIEWED";
DataTable Exportdt = ds.Tables[2];
ExportToExcelFunction("Current Assessment Response Summary", Exportdt, ColName, strBindCols);
}
}
else if (TabContainer1.ActiveTabIndex == 1)
{
ds = (DataSet)ViewState["DSPastResponseSummary"];
if (ds != null && ds.Tables.Count > 0 && ds.Tables[3].Rows.Count > 0)
{
string ColName = "DOMAIN,PERIOD,CIRCLE,DEPARTMENT,SUB-DEPARTMENT,UNIT,ASSESSMENT NUMBER,ASSESSMENTNAME,TOTAL,PENDING,RESPONDED CONTROL,COMPLIANT,NON COMPLIANT,NOT APPLICABLE,TOTAL RESPONDENTS,STATUS,TARGET CLOSURE DATE,REVIEWED";
string strBindCols = "DOMAIN_NAME,PERIOD_NAME,CIRCLE_NAME,DEPARTMENT_NAME,SUBDEPARTMENT_NAME,UNIT_NAME,ASSESMENT_NO,ASSESMENT_NAME,TOTAL_RESPONSE,PENDING_RESPONSE,RESPONDED_CONTROL,COMPLIANT,NON_COMPLIANT,Non Applicable,Total Respondant,STATUS,RESPONSE_DUE_DATE,IS_REVIEWED";
DataTable Exportdt = ds.Tables[3];
ExportToExcelFunction("Past Assessment Response Summary", Exportdt, ColName, strBindCols);
}
}
//if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
//{
// //string ColName = "DOMAIN,PERIOD,CIRCLE,DEPARTMENT,SUB-DEPARTMENT,UNIT,ASSESSMENT NUMBER,ASSESSMENTNAME,STATUS";
// //string strBindCols = "DOMAIN_NAME,PERIOD_NAME,CIRCLE_NAME,DEPARTMENT_NAME,SUBDEPARTMENT_NAME,UNIT_NAME,ASSESMENT_NO,ASSESMENT_NAME,STATUS";
// string ColName = "DOMAIN,PERIOD,CIRCLE,DEPARTMENT,SUB-DEPARTMENT,UNIT,ASSESSMENT NUMBER,ASSESSMENTNAME,TOTAL,PENDING,STATUS";
// string strBindCols = "DOMAIN_NAME,PERIOD_NAME,CIRCLE_NAME,DEPARTMENT_NAME,SUBDEPARTMENT_NAME,UNIT_NAME,ASSESMENT_NO,ASSESMENT_NAME,TOTAL_RESPONSE,PENDING_RESPONSE,STATUS";
// DataTable Exportdt = ds.Tables[0];
// ExportToExcelFunction("Assessment Response Summary", Exportdt, ColName, strBindCols);
//}
Response.Flush();
}
catch (Exception ex)
{
}
}
public void ExportToExcelFunction(string FlName, DataTable mydt, string DispColName, string BindCols)
{
Excel.Application xlObj = new Excel.Application();
object oMissing = System.Reflection.Missing.Value;
xlObj.Visible = false;
//vinod
string filepath = Server.MapPath("Export");
string strFlName = filepath + "\\Master.xlsx";
Excel.Workbook xlWB = xlObj.Workbooks.Open(strFlName, 0, true, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, true, 0, true);
Excel.Worksheet xlSheet = (Excel.Worksheet)xlWB.ActiveSheet;
int cols = mydt.Columns.Count;
int rows = mydt.Rows.Count;
//Added for export to excel
try
{
//For Column
string[] strCols = DispColName.Split(',');
for (int i = 1; i <= strCols.Length; i++)
{
if (strCols[i - 1].Length > 0 && strCols[i - 1] != null)
xlSheet.Cells[1, i] = Convert.ToString(strCols[i - 1]);
}
// for Row
string[] strColBind = BindCols.Split(',');
for (int r = 0; r < rows; r++)
{
for (int c = 0; c < strColBind.Length; c++)
{
if (mydt.Rows[r][strColBind[c]].ToString().ToUpper() == "RELEASED")
xlSheet.Cells[r + 2, c + 1] = "OPEN";
else
xlSheet.Cells[r + 2, c + 1] = mydt.Rows[r][strColBind[c]];
}
}
}
catch (Exception ex)
{
}
String newFlName = "\\" + DateTime.Now.Ticks.ToString() + "_" + FlName + ".xls";
xlWB.SaveAs(filepath + newFlName, Excel.XlFileFormat.xlWorkbookNormal, "", "", false, false, Excel.XlSaveAsAccessMode.xlExclusive, true, false, "", true);
xlWB.Close(true, oMissing, oMissing);
xlObj.Quit();
System.IO.FileInfo file = new System.IO.FileInfo(@"" + filepath + newFlName + "");
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AppendHeader("Content-Disposition", "attachment; filename = " + FlName + ".xls");
Response.AppendHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/download";
Response.WriteFile(file.FullName);
Response.Flush();
Response.Close();
Response.End();
}
【问题讨论】:
标签: c# excel asp.net-3.5