我想将数据导出到PDF / Excel

时间:2015-11-05 06:37:26

标签: c# itext export-to-excel export-to-pdf

我想将数据导出到Excel,因为我已经使用了代码(下面链接),代码正在运行,数据正在导出,但Excel没有下载,请任何人都可以帮我解决问题是什么?

Export data into Excel, Word and PDF with Formatting

这是我如何在我的项目中使用此代码

    foreach (var enq_item in enquiries)
    {
         enquiry_list.Add(new enquiry_master
         {
              enquiry_source_id = enq_item.enquiry_source_id,
               reference_no = enq_item.reference_no,
               assigned_staff_no = enq_item.assigned_staff_no,
               emp_id = enq_item.emp_id,
               status_id = enq_item.status_id,
               remarks = enq_item.remarks,
               system_date_time = enq_item.system_date_time,
               name = enq_item.name,

                departing_from = enq_item.departing_from,
                travelling_to = enq_item.travelling_to,
                departing_date = enq_item.departing_date,
                returning_date = enq_item.returning_date,
                mobile_no = enq_item.mobile_no,
                email = enq_item.email,
         }
    }

    //Get the data from database into datatable
    DataTable dt = ToDataTable(enquiry_list);

    //Create a dummy GridView
    GridView GridView1 = new GridView();
    GridView1.AllowPaging = false;
    GridView1.DataSource = dt;
    GridView1.DataBind();

    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=DataTable.xls");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-excel";
    string filename = "DownloadTest.xls";

    System.IO.StringWriter tw = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
        //Apply text style to each Row
        GridView1.Rows[i].Attributes.Add("class", "textmode");
    }
    GridView1.RenderControl(hw);

    //style to format numbers to string
    string style = @"<style> .textmode { mso-number-format:\@; } </style>";
    Response.Write(style);
    Response.Write(hw.ToString());
    Response.End();

1 个答案:

答案 0 :(得分:0)

其实我们的要求是需要将数据导出到Excel或PDF然后我们根据超出要求认识到最好是PDF所以我尝试了ItextSharp它为我做了我的代码

public string generatePDF()
{
    string HTML = "";   ///Create a html as per our need
    HTML += "<html>";
             ///Update the html here
    HTML += "</html>";

    string pdf_file_path = Request.PhysicalApplicationPath +   "pdf\\quotations\\";    //getting physical application path for save the pdf
    string final_name = "Here pdf name";
    PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(pdf_file_path + final_name, FileMode.Create));
    wri.PageEvent = new ITextEvents();
    doc.Open();

    var content = wri.DirectContent;

    content.MoveTo(28, doc.PageSize.Height - 150);
    content.LineTo(28, doc.PageSize.Height - 200);
    content.Stroke();      //generating line

    content.MoveTo(573, doc.PageSize.Height - 150);
    content.LineTo(573, doc.PageSize.Height - 200);
    content.Stroke();

    HTMLWorker htmlworker = new HTMLWorker(doc);   //here we have to pass created instance of pdfWritter
    htmlworker.SetStyleSheet(style);
    htmlworker.Parse(new StringReader(HTML));  ///here pass the created HTML what we have need in the PDF 
    doc.NewPage();
    doc.Close();

    var json = JsonConvert.SerializeObject(final_name, Newtonsoft.Json.Formatting.Indented, common.JsonSerializeSettings());
    return json;     // retruning the file name 
    Response.Write(doc);
    Response.End();

}  

以上代码对我有用。最后,我使用JavaScript

返回文件名并在浏览器中显示PDF