Excel和Pdf生成中的问题 [英] Problem in Excel and Pdf Generation
问题描述
我有两个页面,其中包含gridviews我在每个页面上有两个按钮,这将有助于打印gridviews为Excel和Pdf
一页包含正常gridview一页面包含多个标题
我使用详细代码创建了Multi标题
public static void GetMultiRowHeader(GridViewRowEventArgs e,SortedList GetCels)
{
if(e.Row.RowType == DataControlRowType.Header)
{
GridViewRow row;
IDictionaryEnumerator enumCels = GetCels.GetEnumerator();
row = new GridViewRow(-1,-1,DataControlRowType.Header,DataControlRowState.Normal);
while(enumCels.MoveNext())
{
String [] count = enumCels.Value.ToString()。Split(Convert.ToChar(,));
TableCell Cell;
Cell = new TableCell();
Cell.RowSpan = Convert.ToInt16(count [2] .ToString());
Cell.ColumnSpan = Convert.ToInt16(count [1] .ToString());
Cell.Controls.Add(new LiteralControl(count [0] .ToString()));
Cell.HorizontalAlign = HorizontalAlign.Center;
Cell.ForeColor = System.Drawing.Color.White;
row.Cells.Add(Cell);
}
e.Row.Parent.Controls.AddAt(0,row);
}
}
protected void GridView1_RowDataBound (对象发送者,GridViewRowEventArgs e)
{
//每当你想添加新的行标题时,你创建新的formatcells变量
// Dim formatCells As New SortedList
SortedList formatCells = new SortedList();
//格式化单元格格式:
//formatCells.Add(<column number> ;,< header name,colspan列数,行数到行数>)
//formatCells.Add(\"1,ROW SPAN,1,2);
//formatCells.Add(\"2,TopGroup,4,1);
// Dim formatcells2 As New SortedList
SortedList formatcells2 = new SortedList();
formatcells2.Add(1,,9,1);
formatcells2.Add(10 ,基础状态(以平方米为单位的范围),2,1);
formatcells2.Add(12,Court case Status,2,1);
formatcells2.Add( 14,分配给Govt Depts(其他和豁免),2,1);
formatcells2.Add(16,,2,1);
// formatcells2.Add( 4,,1,1);
//formatcells2.Add(\"5,Qualification,3,1);
GetMultiRowHeader(e,formatcells2);
GetMultiRowHeader(e,formatCells);
}
我使用fallowing code生成Excel
protected void ImageButtonExcel_Click(object sender,ImageClickEventArgs e)
{
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader(content-disposition,string.Format(attachment; filename = {0},ulcact.xls));
Response.ContentType =application / ms-excel;
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
//将标题行更改回白色
GridView1.HeaderRow.Style.Add(background-color,#FFFFFF);
//将stlye应用于gridview标题单元
for(int i = 0; i< GridView1.HeaderRow.Cells.Count; i ++)
{
GridView1.HeaderRow.Cells [i] .Style.Add(background-color,#507CD1);
}
int j = 1;
//这个循环用于根据特定的行
foreach(GridView1.Rows中的GridViewRow gvrow)将stlye应用于单元格
{
//gvrow.BackColor = Color.White ;
if(j< = GridView1.Rows.Count)
{
if(j%2!= 0)
{
for(int k = 0; k< gvrow.Cells.Count; k ++)
{
gvrow.Cells [k] .Style.Add(background-color,#EFF3FB);
}
}
}
j ++;
}
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
我使用以下代码创建了Pdf
protected void ImageButtonPdf_Click(object sender,ImageClickEventArgs e)
{
Response.ContentType =application / pdf;
Response.AddHeader(content-disposition,attachment; filename = ulcact.pdf);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
GridView1.RenderControl(hw);
GridView1.HeaderRow.Style.Add(width,15%);
GridView1.HeaderRow.Style.Add(font-size,10px);
GridView1.Style.Add(text-decoration,none);
GridView1.Style.Add(font-family,Arial,Helvetica,sans-serif;);
GridView1.Style.Add(font-size,8px);
StringReader sr = new StringReader(sw.ToString());
文件pdfDoc =新文件(PageSize.A2,7f,7f,7f,0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc,Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
我遇到的问题是
1 。在管理模式中,我在gridview中有两列用于选择和更新我希望将它们排除在打印之外作为excel
(我,我只是想从excel中排除前两列)生成Excel表格)
2.在多标题表单中我得到了一个错误的错误(我使用iTextSharp生成Pdf
这个数字PdfPTable构造函数中的列必须大于零。
第129行:PdfWriter.GetInstance(pdfDoc,Response.OutputStream);
行130:pdfDoc.Open();
第131行:htmlparser.Parse(sr);
第132行:pdfDoc.Close();
第133行:Response.Write(pdfDoc);
错误行:源文件:c:\ Users \ ARTISAN \ Documents \ VisualVisual Studio 2008 \\ \ n网站:\\ ULCHYD \ ulcact.aspx.cs行:131
我面临同样的问题我无法解决
对不起我没有阅读你的代码但我在这里有文章出口excel,word和pdf它会帮助你
Excel和字:
< a href =http://engthunder.wordpress.com/2011/10/04/export-gridview-to-excel-and-wor-with-arabic-content-using-itextsharp/> http:// engthunder。 wordpress.com/2011/10/04/export-gridview-to-excel-and-wor-with-arabic-content-using-itextsharp/ [ ^ ] < br $>
PDF:
http://engthunder.wordpress.com/2011/10/03/export-gridview-to-pdf-with- arabic-content-using-itextsharp / [ ^
i have two pages which contains gridviews i have two buttons on each page which will help to print the gridviews as Excel and Pdf
one page contains normal gridview one page contains multi header
I created Multi header by using the faallowing code
public static void GetMultiRowHeader(GridViewRowEventArgs e, SortedList GetCels) { if (e.Row.RowType == DataControlRowType.Header) { GridViewRow row; IDictionaryEnumerator enumCels = GetCels.GetEnumerator(); row = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal); while (enumCels.MoveNext()) { String[] count = enumCels.Value.ToString().Split(Convert.ToChar(",")); TableCell Cell; Cell = new TableCell(); Cell.RowSpan = Convert.ToInt16(count[2].ToString()); Cell.ColumnSpan = Convert.ToInt16(count[1].ToString()); Cell.Controls.Add(new LiteralControl(count[0].ToString())); Cell.HorizontalAlign = HorizontalAlign.Center; Cell.ForeColor = System.Drawing.Color.White; row.Cells.Add(Cell); } e.Row.Parent.Controls.AddAt(0, row); } }
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { //Everytime you want to add new rows header, you creat new formatcells variable //Dim formatCells As New SortedList SortedList formatCells = new SortedList(); //Format cells format:" //formatCells.Add(<column number>, <header Name,number of column to colspan, number of row to rowspan>) //formatCells.Add("1", "ROW SPAN,1,2"); //formatCells.Add("2", "TopGroup,4,1"); // Dim formatcells2 As New SortedList SortedList formatcells2 = new SortedList(); formatcells2.Add("1", ",9,1"); formatcells2.Add("10", "Ground Status (Extent in Sq.Mtrs),2,1"); formatcells2.Add("12", "Court case Status,2,1"); formatcells2.Add("14", "Allotted to Govt Depts(others and Exemptions),2,1"); formatcells2.Add("16", ",2,1"); // formatcells2.Add("4", ",1,1"); //formatcells2.Add("5", "Qualification,3,1"); GetMultiRowHeader(e, formatcells2); GetMultiRowHeader(e, formatCells); }
I Generated Excel by using fallowing code
protected void ImageButtonExcel_Click(object sender, ImageClickEventArgs e) { Response.ClearContent(); Response.Buffer = true; Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "ulcact.xls")); Response.ContentType = "application/ms-excel"; StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); GridView1.AllowPaging = false; GridView1.DataBind(); //Change the Header Row back to white color GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF"); //Applying stlye to gridview header cells for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++) { GridView1.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1"); } int j = 1; //This loop is used to apply stlye to cells based on particular row foreach (GridViewRow gvrow in GridView1.Rows) { //gvrow.BackColor = Color.White; if (j <= GridView1.Rows.Count) { if (j % 2 != 0) { for (int k = 0; k < gvrow.Cells.Count; k++) { gvrow.Cells[k].Style.Add("background-color", "#EFF3FB"); } } } j++; } GridView1.RenderControl(htw); Response.Write(sw.ToString()); Response.End();
I created Pdf by using the fallowing code
protected void ImageButtonPdf_Click(object sender, ImageClickEventArgs e) { Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment;filename=ulcact.pdf"); Response.Cache.SetCacheability(HttpCacheability.NoCache); StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); GridView1.AllowPaging = false; GridView1.DataBind(); GridView1.RenderControl(hw); GridView1.HeaderRow.Style.Add("width", "15%"); GridView1.HeaderRow.Style.Add("font-size", "10px"); GridView1.Style.Add("text-decoration", "none"); GridView1.Style.Add("font-family", "Arial, Helvetica, sans-serif;"); GridView1.Style.Add("font-size", "8px"); StringReader sr = new StringReader(sw.ToString()); Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f); HTMLWorker htmlparser = new HTMLWorker(pdfDoc); PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); htmlparser.Parse(sr); pdfDoc.Close(); Response.Write(pdfDoc); Response.End(); }
The problems i faced are
1.while in the administration mode i have two more columns in the gridview for selecting and updating i wanted to exclude them from printing as excel
(i,e simply i wanted to exclude first two colums from excel when generating Excel sheet)
2.in the multi header form i am getting the fallowing error (i used iTextSharp for generating Pdf
The number of columns in PdfPTable constructor must be greater than zero.
Line 129: PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
Line 130: pdfDoc.Open();
Line 131: htmlparser.Parse(sr);
Line 132: pdfDoc.Close();
Line 133: Response.Write(pdfDoc);
Error Line:Source File: c:\Users\ARTISAN\Documents\Visual Studio 2008\WebSites\ULCHYD\ulcact.aspx.cs Line: 131
i am facing the same problem and i can''t solve it
sorry i did not read your code but i have article here for exporting excel , word and pdf it will help you
Excel and word :
http://engthunder.wordpress.com/2011/10/04/export-gridview-to-excel-and-wor-with-arabic-content-using-itextsharp/[^]
PDF :
http://engthunder.wordpress.com/2011/10/03/export-gridview-to-pdf-with-arabic-content-using-itextsharp/[^]
这篇关于Excel和Pdf生成中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!