You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在ASP.NET中增大ASPxGridView导出PDF的内容尺寸(DevExpress13.2+VS2010)

如何增大ASPxGridView导出PDF时的内容尺寸(DevExpress 13.2 + VS2010)

嘿,我看你用DevExpress 13.2搭配VS2010开发,现有导出PDF的代码,想调整导出内容的尺寸,给你几个实用的方案:

1. 缩小页面边距,扩大内容显示区域

默认的PDF页面边距可能比较宽,缩小边距是最直接增大内容空间的方式。你可以通过ASPxGridViewExporter的Margins属性来设置上下左右边距,单位是百分之一英寸。

修改你的代码,添加边距设置:

protected void Export_Click(object sender, EventArgs e) { 
    ASPxGridView1.DataBind(); 
    ASPxGridViewExporter1.GridViewID = "ASPxGridView1"; 
    ASPxGridViewExporter1.PaperKind = System.Drawing.Printing.PaperKind.A4;
    // 设置边距,这里设为20(相当于0.2英寸),你可以根据需求调整
    ASPxGridViewExporter1.Margins.Left = 20;
    ASPxGridViewExporter1.Margins.Right = 20;
    ASPxGridViewExporter1.Margins.Top = 20;
    ASPxGridViewExporter1.Margins.Bottom = 20;
    ASPxGridViewExporter1.ReportHeader = "hello"; 
    // 注意:这里文件名后缀应该是.pdf,不是.xls,避免文件格式混淆
    ASPxGridViewExporter1.WritePdfToResponse("hello.pdf"); 
}

2. 调整网格列宽,让内容更舒展

如果你的网格列默认宽度较窄,也可以针对性调整列宽,让内容显示更充分。你可以直接在导出前设置ASPxGridView的列宽,或者通过RenderBrick事件精细调整单元格宽度:

方式一:直接设置GridView列宽

在DataBind后,给目标列设置宽度:

ASPxGridView1.DataBind();
// 比如给FieldName为"ProductName"的列设置宽度为150像素
ASPxGridView1.Columns["ProductName"].Width = Unit.Pixel(150);

方式二:通过RenderBrick事件调整

先在页面给ASPxGridViewExporter绑定事件:

<dx:ASPxGridViewExporter ID="ASPxGridViewExporter1" runat="server" 
    OnRenderBrick="ASPxGridViewExporter1_RenderBrick">
</dx:ASPxGridViewExporter>

然后在后台编写事件处理代码:

protected void ASPxGridViewExporter1_RenderBrick(object sender, ASPxGridViewExportRenderingEventArgs e)
{
    // 针对特定列调整宽度,这里以FieldName为"ProductName"的列为例
    if (e.Column != null && e.Column.FieldName == "ProductName")
    {
        e.BrickStyle.Width = Unit.Pixel(150);
    }
    // 也可以批量调整所有列的宽度
    // if (e.Column != null) e.BrickStyle.Width = Unit.Pixel(120);
}

3. 整体缩放内容适配页面

如果想让内容整体放大,或者自动适配页面宽度,可以使用PdfExportOptions的缩放设置:

protected void Export_Click(object sender, EventArgs e) { 
    ASPxGridView1.DataBind(); 
    ASPxGridViewExporter1.GridViewID = "ASPxGridView1"; 
    ASPxGridViewExporter1.PaperKind = System.Drawing.Printing.PaperKind.A4;
    ASPxGridViewExporter1.ReportHeader = "hello";
    // 方式一:自动缩放内容以适配页面宽度
    ASPxGridViewExporter1.PdfExportOptions.ScaleMode = PdfExportScaleMode.ScaleToPageWidth;
    // 方式二:直接设置缩放比例,比如放大20%(1.2倍)
    // ASPxGridViewExporter1.PdfExportOptions.Scale = 1.2f;
    ASPxGridViewExporter1.WritePdfToResponse("hello.pdf"); 
}

最后提个小细节:你现有代码里WritePdfToResponse的文件名写的是hello.xls,这会导致下载的文件后缀错误,建议改成hello.pdf,避免用户打开时出现格式问题。

内容的提问来源于stack exchange,提问作者Amal Ps

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:43:53