62,046
社区成员
发帖
与我相关
我的任务
分享
protected void LinkButton1_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
LoginSession model = Session["LoginSession"] as LoginSession;
string DBType = ConfigurationManager.AppSettings["DBtype"];
if (DBType == "ora")
{
ds = DbHelperOra.Query(refsql, model.getJndiName());
}
else
{
ds = DbHelperSQL.Query(refsql, model.getJndiName());
}
//Page.RegisterStartupScript("asddas", "<script>hideDiv('pop-div');</script>");
//ClientScript.RegisterStartupScript(ClientScript.GetType(), "asddas", "<script>alert('1');$(\"#mask\").remove();$(\"#pop-div\").animate({left: 0, top: 0, opacity: \"hide\" }, \"slow\");</script>");
exportToExcel(ds.Tables[0], "收集整编");
Response.Write("<script>alert('导出成功!');</script>");
}
///
/// 将DataTable导入到Excel中
///
/// 数据源
/// 目标Excel
public void exportToExcel(DataTable dt, string Title)
{
string strRealFile = HttpUtility.UrlEncode(Title, System.Text.Encoding.UTF8);
HttpResponse resp = System.Web.HttpContext.Current.Response;
string ExcelName = strRealFile + DateTime.Now.ToString("yyyyMMddHHmmss");
resp.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
resp.AppendHeader("Content-Disposition", "attachment;filename=" + ExcelName + ".xls");
string colHeaders = "", ls_item = "";
DataRow[] myRow = dt.Select();//可以类似dt.Select("id>10")之形式达到数据筛选目的
int i = 0;
int cl = dt.Columns.Count;
//取得数据表各列标题,各标题之间以t分割,最后一个列标题后加回车符
//resp.Write("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /></head><body><table border=1><tr ");
//添加网格
resp.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />" +
"<!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>123</x:Name><x:WorksheetOptions><x:Print><x:ValidPrinterInfo /></x:Print></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table><tr ");
List<int> countnum = new List<int>();
List<string> countname = new List<string>();
for (i = 0; i < cl; i++)
{
for(int k=0;k<ds1_print.Tables[0].Rows.Count;k++)
{
if (dt.Columns[i].Caption.ToString() == ds1_print.Tables[0].Rows[k]["FIELDNAME"].ToString())
{
countnum.Add(i);
countname.Add(ds1_print.Tables[0].Rows[k]["FIELDNAME"].ToString());
}
}
}
for (int k = 0; k < countname.Count; k++)
{
for (i = 0; i < dt.Columns.Count; i++)
{
if (countname[k]==dt.Columns[i].Caption.ToString())
{
string newname = ds1_print.Tables[0].Select("FIELDNAME='" + dt.Columns[i].Caption.ToString() + "'")[0]["DISPNAME"].ToString();
colHeaders += "<th>" + newname + "</th>";
}
}
}
resp.Write(colHeaders + "</tr>");
//向HTTP输出流中写入取得的数据信息
//逐行处理数据
foreach (DataRow row in myRow)
{
//当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
ls_item = "<tr>";
for (int k = 0; k < countname.Count; k++)
{
for (i = 0; i < cl; i++)
{
if (countname[k] == dt.Columns[i].Caption.ToString())
{
if (countname[k] == "FLH" || countname[k] == "CWRQ" || countname[k] == "PSRQ" || countname[k] == "GDRQ" || countname[k] == "PRJCODE")
{
if (i == (cl - 1))//最后一列,加n
{
ls_item += "<td>'" + row[i].ToString() + "</td></tr>";
}
else
{
ls_item += "<td>'" + row[i].ToString() + "</td>";
}
}
else
{
if (i == (cl - 1))//最后一列,加n
{
ls_item += "<td>" + row[i].ToString() + "</td></tr>";
}
else
{
ls_item += "<td>" + row[i].ToString() + "</td>";
}
}
}
}
}
resp.Write(ls_item);
}
resp.Write("</table></body></html>");
//resp.Write("<script>alert('导出成功!');</script>");
resp.End();
//Response.Write("<script>alert('导出成功!');</script>");
}