C#:将网格数据导出到 Excel 时保留前导零

问题描述 投票:0回答:1

我试图弄清楚如何在将数据从网格导出到 Excel 时保留前导 0,方法是通过操作每个单元格的值或为整个列设置格式。 我发现了一些在值前面添加“”的建议,但这没有任何效果。 到目前为止,我找到的唯一解决方案是在值前面添加撇号,这可行,但我真的不想要 excel 文件中的 ' 。

这是我尝试过的代码解决方案之一:

protected void btnExport_Click(object sender, EventArgs e)
{
    RGData.ExportSettings.FileName = "FileName" + DateTime.Now.ToShortDateString() + "";
    RGData.ExportSettings.OpenInNewWindow = true;
    RGData.ExportSettings.ExportOnlyData = true;
    RGDataExport.ExportSettings.Excel.Format = GridExcelExportFormat.Xlsx;

    foreach (GridDataItem item in RGData.MasterTableView.Items)
    {
        TableCell cell = item["Number"];

        if (!string.IsNullOrEmpty(cell.Text))
        {
            cell.Text = "\t" + cell.Text;
        }
    }

    RGData.MasterTableView.ExportToExcel();
}

如有任何意见,我们将不胜感激。

问候, 麦兹

c# excel
1个回答
0
投票

找到解决方案。

protected void btnExport_Click(object sender, EventArgs e)
{
    RGData.ExportSettings.FileName = "FileName" + DateTime.Now.ToShortDateString() + "";
    RGData.ExportSettings.OpenInNewWindow = true;
    RGData.ExportSettings.ExportOnlyData = true;
    RGDataExport.ExportSettings.Excel.Format = GridExcelExportFormat.Xlsx;

    foreach (GridDataItem c in RGData.MasterTableView.Items)
    {
        TableCell cell = c["Number"];

        cell.Style["mso-number-format"] = "\\@";
    }

    RGData.MasterTableView.ExportToExcel();
}
© www.soinside.com 2019 - 2024. All rights reserved.