c#语言编写的程序,Microsoft Visual Studio 2005平台,为什么生成的excel工作簿只有一个表!表名还是默认

2024-12-23 14:18:21
推荐回答(1个)
回答1:

#region 导出excel文档

private void ToExcel(string oId, string startTime, string endTime, string uno)
{
string fileName = "";
string oName = "";
if (ddlCity.SelectedItem.Value == "0")
{
oName = "全部";
}
else
{
oName = ddlCity.SelectedItem.Text.Trim();
}
if (startTime != null && endTime != null && startTime != "" && endTime != "")
{
fileName = Convert.ToDateTime(startTime).ToString("yyyy年MM月dd日") + "至" + Convert.ToDateTime(endTime).ToString("yyyy年MM月dd日") + oName + "答题情况统计";
}
else
{
fileName = oName + "答题情况统计";
}

DataTable dtVUserExamInfo = DAL.V_UserExamInfoDAL.GetUserExamCount(oId, startTime, endTime, uno); ;

if (dtVUserExamInfo.Rows.Count > 0)
{
StringWriter sw = new StringWriter();
sw.WriteLine("");
sw.WriteLine(">");
sw.WriteLine("");
sw.WriteLine("");
sw.WriteLine("");
sw.WriteLine("

");

sw.WriteLine(" ");
sw.WriteLine(" ");
sw.WriteLine(" ");

sw.WriteLine(" ");
sw.WriteLine(" ");
sw.WriteLine(" ");

if (startTime != null && endTime != null && startTime != "" && endTime != "")
{
sw.WriteLine(" ");
sw.WriteLine(" ");
sw.WriteLine(" ");
}

//列头名称
sw.WriteLine(" ");
sw.WriteLine(" ");
sw.WriteLine(" ");
sw.WriteLine(" ");
sw.WriteLine(" ");
sw.WriteLine(" ");
sw.WriteLine(" ");
sw.WriteLine(" ");

for (int i = 0; i < dtVUserExamInfo.Rows.Count; i++)
{
sw.WriteLine("");
sw.WriteLine("");
sw.WriteLine("");
sw.WriteLine("");
sw.WriteLine("");
sw.WriteLine("");
sw.WriteLine("");

sw.WriteLine(" ");
}
sw.WriteLine("
" + fileName + "
参与情况:" + dtVUserExamInfo.Rows.Count.ToString() + "人参加测试
测试时间:" + Convert.ToDateTime(startTime).ToString("yyyy年MM月dd日") + "至" + Convert.ToDateTime(endTime).ToString("yyyy年MM月dd日") + "
编号工号姓名单位测试次数平均成绩
" + (i + 1).ToString() + "" + dtVUserExamInfo.Rows[i]["UNo"].ToString() + "" + dtVUserExamInfo.Rows[i]["UName"].ToString() + "" + dtVUserExamInfo.Rows[i]["OName"].ToString() + "" + dtVUserExamInfo.Rows[i]["ECount"].ToString() + "" + dtVUserExamInfo.Rows[i]["AverageScore"].ToString() + "
");
sw.WriteLine("");
sw.WriteLine("");
sw.Close();
Response.Clear();
Response.Buffer = true;
Response.Charset = "UTF-8";
this.EnableViewState = false;
//这样的话,可以设置文件名为中文,且文件名不会乱码。其实就是将汉字转换成UTF8
Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ".xls");
Response.ContentType = "application/ms-excel";
//Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
Response.Write(sw);
Response.End();
}
}
//必须有下面这句!否则不会通过!
public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for
}
#endregion