【问题标题】:C# Excel Interop chart legends displays string format instead of valuesC# Excel 互操作图表图例显示字符串格式而不是值
【发布时间】:2021-03-24 13:00:30
【问题描述】:

所以我正在尝试编写一个函数来将工作簿中某些工作表的打印区域转换为图像文件。

这些工作表包含一些图表,在一些图表上,图像显示的图例如下所示:

Result

当我在 Excel 中打开它时,它显示正常。另外值得一提的是,我在匈牙利使用 Excel,但是当转换为图片时,某些月份在其他图表上以英文显示。

C# 控制台应用程序的参考:

using System;
using xls = Microsoft.Office.Interop.Excel;
using System.Drawing;
using System.Windows.Forms;
using System.Globalization;

public static bool makeimage(string excelpath,string savefolder) {

    bool ret = true;

    string fileNameToProcess = excelpath;            
    xls.Application oExcel = new xls.Application();
    xls.Workbook wb = null;
    try
    {
        wb = oExcel.Workbooks.Open(fileNameToProcess.ToString(), false, true, Type.Missing, "", "", true, xls.XlPlatform.xlWindows, "", false, false, 0, false, true, 0);
        wb.RefreshAll();
        xls.Sheets sheets = wb.Worksheets as xls.Sheets;
        for (int i = 1; i <= sheets.Count; i++)
        {
            xls.Worksheet sheet = sheets[i];
            if (sheet.Visible == 0) { continue; }
            xls.Range range = null;
            Console.WriteLine(sheet.Name);
            try
            {                        
                range = sheet.Range[sheet.PageSetup.PrintArea];                     
                sheet.Activate();
                sheet.Calculate();
                sheet.Evaluate(range);
                oExcel.ActiveWindow.View = xls.XlWindowView.xlNormalView;


            }
            catch (Exception e)
            {
                continue;
            }
            Console.WriteLine("Converting range to image: " + range.CopyPicture(xls.XlPictureAppearance.xlScreen, xls.XlCopyPictureFormat.xlBitmap).ToString());

            if (Clipboard.ContainsImage())
            {
                string imagepath = savefolder + sheet.Name + ".PNG";
                if (System.IO.File.Exists(imagepath))
                    System.IO.File.Delete(imagepath);
                Image imgRange1 = Clipboard.GetImage();
                imgRange1.Save(imagepath, System.Drawing.Imaging.ImageFormat.Png);

            }
            sheets[i].Delete();
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
        ret = false;
        //throw;
    }
    finally
    {
        wb.Close(false);
        oExcel.Quit();
        oExcel = null;
    }
    
    return ret; 
}

当我尝试将这些工作表转换为 PDF 时出现同样的问题。

public static bool makePDF(string excelpath, string savefolder)
{

    bool ret = true;

    string fileNameToProcess = excelpath;
    //Start Excel and create a new document.
    xls.Application oExcel = new xls.Application();
    xls.Workbook wb = null;
    try
    {
        wb = oExcel.Workbooks.Open(fileNameToProcess.ToString(), false, true, Type.Missing, "", "", true, xls.XlPlatform.xlWindows, "", false, false, 0, false, true, 0);
        wb.RefreshAll();
        xls.Sheets sheets = wb.Worksheets as xls.Sheets;
        for (int i = 1; i <= sheets.Count; i++)
        {
            xls.Worksheet sheet = sheets[i];
            if (sheet.Visible == 0) { continue; }
            xls.Range range = null;
            Console.Write(sheet.Name);
            try
            {                      
             
                range = sheet.Range[sheet.PageSetup.PrintArea];
                //Console.WriteLine(range.Address.ToString());
                sheet.Activate();
                sheet.Calculate();
                sheet.Evaluate(range);
                oExcel.ActiveWindow.View = xls.XlWindowView.xlNormalView;
                sheet.ExportAsFixedFormat(xls.XlFixedFormatType.xlTypePDF, savefolder+sheet.Name, xls.XlFixedFormatQuality.xlQualityStandard, false, false, 1, 1, false);
                
                Console.WriteLine(" has been converted");
                

            }
            catch (Exception e)
            {
                Console.WriteLine(" has not been converted");
                continue;
            }
           

            //}
            sheets[i].Delete();
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
        ret = false;
        //throw;
    }
    finally
    {
        wb.Close(false);
        oExcel.Quit();
        oExcel = null;
    }

    return ret;
}

有人对我缺少什么或我可以尝试什么有任何建议吗?

【问题讨论】:

    标签: c# excel charts interop


    【解决方案1】:

    我不知道为什么会发生这种情况,但我确实设法通过将 excel 文档保存到不同的位置来解决问题,然后复制每个使用范围的工作表,并将其作为值粘贴回来,然后保存图片或PDF。

    【讨论】:

      猜你喜欢
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多