DataSet 写入 Excel

在 农科院项目中 碰到这个问题

之前 用的 DataView  到 Excel  发现 数据 不全,因为翻页 之后改成 这样

源代码:

代码
try
{
string sql;
string Sdate = Convert.ToDateTime(S_Date.Value).ToShortDateString() + " " + ddl_Shour.SelectedValue + ":00:00";

string Edate = Convert.ToDateTime(D_Date.Value).ToShortDateString() + " " + ddl_Dhour.SelectedValue + ":59:59";
string strWhere;

string strOrder;

strWhere
= " Customer_ID=" + CustomerID + " and CollHisData_Time>'" + Sdate + "' and CollHisData_Time<='" + Edate + "'";


//端点
if (NodeType == "0")
{
strWhere
= strWhere + " and TDC_Terminal.Terminal_ID=" + NodeId;


strWhere
= strWhere + "";

}
//测点
if (NodeType == "1")
{
strWhere
= strWhere + " and TDC_Collpt.CollPt_ID='" + HttpUtility.UrlDecode(NodeId) + "'";
strWhere
= strWhere + "";
}

//测点类型
if (ddl_PtType.SelectedValue == "0")
{
strWhere
= strWhere + "";
}
else
{
strWhere
= strWhere + " and TDC_CollPt.CollPt_ID in (select Collpt_ID from TDC_Collpt where PtType_Id='" + ddl_PtType.SelectedValue + "')";


}


strOrder
= " order by CollHisData_Time desc ";

sql
= "SELECT TDC_CollPt.CollPt_Name, Customer_ID, TDC_Terminal.Terminal_Name, ";
sql
= sql + " TDC_CollPt.Terminal_ID,TDC_HistoryData.CollPt_ID, ";
sql
= sql + " TDC_CollPt.PtType_ID, TDC_PtType.PtType_Name, ";
sql
= sql + " TDC_PtType.PtType_Unit, TDC_HistoryData.CollHisData_Time,CollHisData_Id, ";
sql
= sql + " TDC_HistoryData.CollHisData_Data";
sql
= sql + " FROM TDC_CollPt INNER JOIN";
sql
= sql + " TDC_Terminal ON";
sql
= sql + " TDC_CollPt.Terminal_ID = TDC_Terminal.Terminal_ID INNER JOIN";
sql
= sql + " TDC_PtType ON ";
sql
= sql + " TDC_CollPt.PtType_ID = TDC_PtType.PtType_ID INNER JOIN";
sql
= sql + " TDC_HistoryData ON TDC_CollPt.CollPt_ID = TDC_HistoryData.CollPt_ID and " + strWhere+ strOrder;



DataSet ds
= DBUtility.SQLHelper.ExecuteDataset(DBUtility.SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, sql);


string[] questionName = { "Terminal_Name", "CollPt_Name", "PtType_Name", "CollHisData_Data","PtType_Unit" , "CollHisData_Time"};

string[] questionShowName = { "端点名称", "测点名称", "测点类型", "采集值","单位", "采集时间" };
Response.ClearContent();
this.Response.ContentType = "application/vnd.ms-excel";
HttpServerUtility server
= this.Server;
string encodingfilename = server.UrlPathEncode("FileEx");
this.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(
Convert.ToDateTime(S_Date.Value).ToShortDateString()
+ " " + ddl_Shour.SelectedValue.PadLeft(2,'0')
+ "~" + Convert.ToDateTime(D_Date.Value).ToShortDateString() + " " + ddl_Dhour.SelectedValue.PadLeft(2,'0')
+ "的查询报表", System.Text.Encoding.UTF8) + ".xls");
this.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
string str = "";



for (int i = 0; i < questionShowName.Length; i++)
{
string flg = i ==questionShowName.Length - 1 == true ? "\n" : "\t";
str
+=questionShowName[i] + flg;
}
this.Response.Write(str);

for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
string newrow = string.Empty;
for (int j = 0; j < questionName.Length; j++)
{
string flg = j == questionName.Length - 1 == true ? "\n" : "\t";
newrow
+= ds.Tables[0].Rows[i][questionName[j]].ToString().Replace("&nbsp;", "") + flg;
}
this.Response.Write(newrow);
}

this.Response.End();
Response.Flush();






}
catch
{ }

 

posted @ 2010-02-24 10:05  Sue_娜  阅读(258)  评论(0编辑  收藏  举报