开发者

iTextSharp EXCEL output

开发者 https://www.devze.com 2023-02-12 14:15 出处:网络
I am using the following code to output data from a number of tables in my database to an excel document.

I am using the following code to output data from a number of tables in my database to an excel document.

Protected void btnExcelExport_Click(object sender, EventArgs e)
{

string strQuery = "SELECT开发者_如何学Go 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();
}

The output is as follows:

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

What I would like to do is 3 things. Many thanks for your help!

1) Change the header names which are clearly coming from the database fields (ie. from StoryCategoryID to Story Type), 2) For StoryCategoryID, if 1 is retrieved - display 'Negative' and if 0 is retrieved - display 'Positive' instead. 3) Have the output in the following format:

Story | Story Type | Creation Date | Company Role | Tag 1 | Tag 2 | Tag 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


You can modify the column names of the data table. Use foreach(DataColumn col in dt.Columns) then use col.ColumnName

You can access and modify the data for the specific column for your criteria using (Convert.ToInt16(col[n]) != 1)? "Positive" : "Negative" inside a loop. Use dt.Rows.Count for the loop's condition.

Hope this helps.

You can use this inside the foreach loop:

if (col.ColumnName == "StoryCategoryId") //Or "Story Type"
for(int rowCount = 0; rowCount < table.Rows.Count; rowCount++)
{
    col[rowCount] = (Convert.ToInt16(col[rowCount]) == 0) ? "Positive" : "Negative";
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号