0
我使用下面的代码的输出数据从多个表中我的数据库到Excel文件:数据集输出至Excel问题
Protected void btnExcelExport_Click(object sender, EventArgs e)
{
string strQuery = "SELECT s.Story, s.StoryCategoryID, CONVERT(VARCHAR(10),
s.CreationDate, 103) AS CreationDate, m.CompanyRole, af.Name FROM Story s INNER JOIN
ProjectIterationMember pm ON pm.ProjectIterationMemberID = s.ProjectIterationMemberID
INNER JOIN Iterations i ON i.ProjectIterationID = pm.ProjectIterationID INNER JOIN
Member m ON m.MemberID = pm.MemberID INNER JOIN ProjectStoryFactors psf ON psf.StoryID =
s.StoryID INNER JOIN AgileFactors af ON af.AgileFactorID = psf.AgileFactorID WHERE
i.ProjectID = '" + proj_id + "'";
SqlCommand cmd = new SqlCommand(strQuery);
DataTable dt = GetData(cmd);
GridView GridView1 = new GridView();
GridView1.AllowPaging = false;
GridView1.DataSource = dt;
GridView1.DataBind();
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition","attachment;filename=RetroCloud" +
DateTime.Now.Ticks + ".xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridView1.Rows[i].Attributes.Add("class", "textmode");
}
GridView1.RenderControl(hw);
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
输出如下:
Story | StoryCategoryID | CreationDate | CompanyRole | Name
negative iii | 1 | 21/02/2011 | Business Analyst | Project Duration
negative iii | 1 | 21/02/2011 | Business Analyst | Team Size
negative iii | 1 | 21/02/2011 | Business Analyst | Process
negative ccc | 1 | 22/02/2011 | Admin | Workspace Layout
negative ccc | 1 | 22/02/2011 | Admin | Organisational and Reporting Structure
negative ccc | 1 | 22/02/2011 | Admin | Process
我想这样做的两件事情。示例代码,将不胜感激 - 这是wreching我的头!非常感谢您的帮助!
1)对于StoryCategoryID,如果1被检索 - 显示“否定”,并且如果0检索 - 显示“正”代替。
2)输出格式如下:
故事|故事类型|创建日期|公司角色|标记1 |标签2 |标签3
negative iii | 1 | 21/02/2011 | Business Analyst | Project Duration | Team Size | Process
negative ccc | 1 | 22/02/2011 | Admin | Workspace Layout | Organisational | Process
对于部分1),你可以轻松地添加case语句对您的SQL查询(侧面说明:您应该查看参数化查询) - 选择案例(s.StoryCategoryID)当'1'然后'负'时'0'然后'正面'其他'未知'结束为StoryCategoryId – kd7 2011-02-23 19:12:57