2011-02-23 19 views
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 
+0

對於部分1),你可以輕鬆地添加case語句對您的SQL查詢(側面說明:您應該查看參數化查詢) - 選擇案例(s.StoryCategoryID)當'1'然後'負'時'0'然後'正面'其他'未知'結束爲StoryCategoryId – kd7 2011-02-23 19:12:57

回答

0

好吧,也許不是最好的解決辦法,但是這可能會做你想要什麼:

SELECT 
    Story 
, CASE StoryCategoryID WHEN 1 THEN 'Negative' WHEN 0 THEN 'Positive' ELSE CAST(StoryCategoryID as varchar(10)) as StoryCategoryID 
, CreationDate 
, CompanyRole 
, MAX(CASE WHEN Z.MinName = af2.Name THEN af2.Name ELSE '' END) as Tag1 
, MAX(CASE WHEN Z.MinName <> af2.Name AND Z.MaxName <> af2.Name THEN af2.Name ELSE '' END) as Tag2 
, MAX(CASE WHEN Z.MaxName = af2.Name THEN af2.Name ELSE '' END) as Tag3 
FROM (
SELECT 
    s.Story 
, s.StoryCategoryID 
, CONVERT(VARCHAR(10), s.CreationDate, 103) AS CreationDate 
, m.CompanyRole 
, af.AgileFactorID 
, MIN(af.Name) MinName 
, MAX(af.Name) MaxName 
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 + "'"; 
GROUP BY s.Story, s.StoryCategoryID, CONVERT(VARCHAR(10), s.CreationDate, 103), m.CompanyRole, af.AgileFactorID 
) Z 
INNER JOIN AgileFactors af2 
    ON af2.AgileFactorID = Z.AgileFactorID 
GROUP BY Story, StoryCategoryID, CreateDate, CompanyRole