简体   繁体   中英

How can I get the path of exported file (telerik RadGrid exporting)?

I'm exporting a PDF file using

RadGrid1.MasterTableView.ExportToPdf()

This is a sub. My question is: Can I get the exported file path somehow?

Thank you in advance,

Lajos Árpád.

When you export, the NeedDataSource event will fire. Store the data into a simple ADO.NET Datatable.

protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        DataTable table = new DataTable();
        table.Columns.Add("Col1", typeof(double));
        for (int i = 0; i < 2; i++)
            table.Rows.Add(i);

        (sender as RadGrid).DataSource = table;
    }

Self Explanatory. Removes the text boxes, leaves just the text.

public void ReplaceTextBoxes(Control ctrl)
{
    var q = new Stack<Control>(ctrl.Controls.OfType<Control>());
    while (q.Count > 0)
    {
        Control control = q.Pop();
        if (control is ITextControl)
        {
            ctrl.Controls.Add(new LiteralControl((control as ITextControl).Text));
            ctrl.Controls.Remove(control);
        }
        if (control.HasControls())
            ReplaceTextBoxes(control);
    }
}

To store on the server, add an OnGridExporting event.

protected void RadGrid1_GridExporting(object sender, GridExportingArgs e)
{
    using (FileStream fs = File.Create(Request.PhysicalApplicationPath + "RadGrid.pdf"))
    {
        byte[] output = Encoding.GetEncoding(1252).GetBytes(e.ExportOutput);
        fs.Write(output, 0, output.Length);
    }

    Response.Redirect(Request.Url.ToString());
}

You can change "RadGrid.pdf" to whatever you want it to be called.

And finally a button to make all this happen (or you can just call the ExportToPdf function wherever.

protected void Button1_Click(object sender, EventArgs e)
{
    foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
        ReplaceTextBoxes(item);
    RadGrid1.MasterTableView.ExportToPdf();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM