test.zip (241.7 KB)
image.png (54.5 KB)
image (1).png (88.9 KB)
如图所示 麻烦帮忙看下这是为什么
您在将 Excel 转成 PDF(或直接使用 Aspose.PDF 创建图表)时,数据标签(Data Label) 在 PDF 中未能正常显示,而在 Excel 中显示正常。
下面列出常见的导致这种现象的原因以及对应的解决方案。请根据您实际的实现方式(Aspose.Cells → PDF 还是 Aspose.PDF → Chart)选取相应的步骤。
| 原因 | 说明 |
|---|---|
| 图表的 DataLabel 未显式开启 | 即使在 Excel UI 中打开了数据标签,代码生成的工作簿如果没有设置 ShowDataLabels,导出时也会被忽略。 |
| 旧版库不完全支持 DataLabel | 早期版本的 Aspose.Cells 在 PDF 导出时对某些图表类型(如 Pie、Bar)不完整。 |
| 保存选项未启用 | PdfSaveOptions 中的 EmbedStandardFonts、Compliance 等参数不会影响 DataLabel,但使用默认选项可以避免意外丢失。 |
using Aspose.Cells;
using Aspose.Cells.Charts;
class Program
{
static void Main()
{
// ① 加载或创建工作簿
Workbook wb = new Workbook();
Worksheet sheet = wb.Worksheets[0];
// ② 添加示例数据
sheet.Cells["A1"].PutValue("Category");
sheet.Cells["B1"].PutValue("Value");
sheet.Cells["A2"].PutValue("A"); sheet.Cells["B2"].PutValue(30);
sheet.Cells["A3"].PutValue("B"); sheet.Cells["B3"].PutValue(70);
// ③ 创建图表(以 Pie 为例)
int chartIdx = sheet.Charts.Add(ChartType.Pie, 5, 0, 20, 10);
Chart chart = sheet.Charts[chartIdx];
chart.NSeries.Add("B2:B3", true);
chart.NSeries.CategoryData = "A2:A3";
// ④ 开启并自定义 DataLabel
chart.ShowDataLabels = true; // 总开关
chart.DataLabels.Position = LabelPosition.Center; // 位置(可选)
chart.DataLabels.ShowCategoryName = true; // 显示类别名称
chart.DataLabels.ShowValue = true; // 显示数值
chart.DataLabels.ShowPercentage = true; // 显示百分比(针对饼图)
// ⑤ 导出为 PDF(使用最新版本的 Aspose.Cells)
wb.Save("ChartWithLabels.pdf", SaveFormat.Pdf);
}
}
要点说明
chart.ShowDataLabels = true; 必须在保存前设置。chart.DataLabels 可以细化显示内容(类别、数值、百分比、系列名称等)。文档链接:
| 原因 | 说明 |
|---|---|
未设置 DataLabel.Show |
默认情况下,Aspose.PDF 的 Chart 对象不渲染数据标签,需要手动打开。 |
| 使用了不支持 DataLabel 的图表类型 | 某些 3D 或组合图在旧版中不完整。 |
PDF 保存前未调用 Chart.Draw() |
在手动绘制流程中,必须在 document.Pages.Add(chart) 前完成所有属性设置。 |
using Aspose.Pdf;
using Aspose.Pdf.Text;
using Aspose.Pdf.Drawing;
class PdfChartDemo
{
static void Main()
{
// ① 创建 PDF 文档
Document pdfDoc = new Document();
Page page = pdfDoc.Pages.Add();
// ② 创建柱状图(BarChart 为示例,其他 ChartType 类似)
BarChart barChart = new BarChart
{
X = 50,
Y = 500,
Width = 400,
Height = 300,
Title = new ChartTitle { Text = "Sales by Quarter" }
};
// ③ 添加数据点
BarSeries series = new BarSeries
{
Color = Color.Blue,
DataLabels = new DataLabels
{
Show = true, // 开启数据标签
ShowValue = true, // 显示数值
Font = new Font(FontFamily.Helvetica, 9, FontStyle.Bold)
}
};
series.DataPoints.Add(new DataPoint(120));
series.DataPoints.Add(new DataPoint(150));
series.DataPoints.Add(new DataPoint(180));
series.DataPoints.Add(new DataPoint(210));
barChart.Series.Add(series);
// ④ 添加 X 轴标签(可选)
barChart.CategoryAxis.Labels.Add(new CategoryAxisLabel { Text = "Q1" });
barChart.CategoryAxis.Labels.Add(new CategoryAxisLabel { Text = "Q2" });
barChart.CategoryAxis.Labels.Add(new CategoryAxisLabel { Text = "Q3" });
barChart.CategoryAxis.Labels.Add(new CategoryAxisLabel { Text = "Q4" });
// ⑤ 将图表加入页面并保存
page.Paragraphs.Add(barChart);
pdfDoc.Save("BarChartWithLabels.pdf");
}
}
要点说明
DataLabels.Show = true 与 DataLabels.ShowValue = true 必须一起使用才能在 PDF 中看到数值。Series 上设置相同的 DataLabels。文档链接:
| 检查项 | 操作 |
|---|---|
| 库版本 | 确保使用最新的 Aspose.Cells / Aspose.PDF 发行版。旧版可能缺少对特定图表属性的支持。 |
| 图表类型 | 某些组合图或 3D 图在 PDF 导出时不完整;尝试改为普通 2D 图表验证是否为类型问题。 |
| 字体嵌入 | 若使用自定义字体,请在 PdfSaveOptions(或 Document 的 Fonts)中嵌入,防止标签因缺字而不显示。 |
| 保存方式 | 对于 Excel → PDF,直接使用 Workbook.Save(..., SaveFormat.Pdf);不要先手动转换为图片后再生成 PDF。 |
| 样式冲突 | 检查是否在代码中对 DataLabels.Font.Color、BackgroundColor 等做了透明或与背景相同的设置。 |
23.12.0)。test.zip 中如果包含 .csproj 或完整解决方案,请确认已解压后检查 packages.config / *.csproj 中的版本信息。有了以上信息,我们可以给出更精准的定位与修复建议。
祝您开发顺利! 若还有其他细节需要帮助,请随时告知。
— Aspose Technical Support
感谢提供模板Excel文件和截图。
根据您提到的情况,我通过将您的模板Excel文件转换为PDF,复现了该问题。我发现数据标签在Excel转PDF过程中未正确呈现。
Workbook workbook = new Workbook("e:\\test2\\test.xlsx");
workbook.Save("e:\\test2\\test.pdf");
我们需要对该问题进行全面评估。我们已在内部问题跟踪系统中创建了以下新工单,并将根据Free Support Policies中提到的条款交付修复。
问题ID:CELLSNET-59419
如果您需要优先支持服务以及直接与我们的付费支持管理团队联系,可以获取Paid Support Services。