Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用C将Excel转换为pdf时添加页码、页眉和页脚#_C#_Excel_Pdf - Fatal编程技术网

C# 使用C将Excel转换为pdf时添加页码、页眉和页脚#

C# 使用C将Excel转换为pdf时添加页码、页眉和页脚#,c#,excel,pdf,C#,Excel,Pdf,我一直在一个C#MVC应用程序(Visual Studio 2012)中工作。我需要创建一个Excel文件,并使用c#将其导出为pdf格式。但是,我未来的pdf文档必须有页眉、页脚和页码。如何在c#中实现它们?提前感谢您的帮助。我用于将带图表的Excel文档转换为pdf的代码如下: Excel.Application xlApp; Excel.Workbook xlWorkBook; Excel.Worksheet xlWorkSheet;

我一直在一个C#MVC应用程序(Visual Studio 2012)中工作。我需要创建一个Excel文件,并使用c#将其导出为pdf格式。但是,我未来的pdf文档必须有页眉、页脚和页码。如何在c#中实现它们?提前感谢您的帮助。我用于将带图表的Excel文档转换为pdf的代码如下:

        Excel.Application xlApp;
        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet;
        object misValue = System.Reflection.Missing.Value;

        xlApp = new Excel.Application();
        xlWorkBook = xlApp.Workbooks.Add(misValue);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

        //add data 
        xlWorkSheet.Cells[1, 1] = "";
        xlWorkSheet.Cells[1, 2] = "Student1";
        xlWorkSheet.Cells[1, 3] = "Student2";
        xlWorkSheet.Cells[1, 4] = "Student3";

        xlWorkSheet.Cells[2, 1] = "Term1";
        xlWorkSheet.Cells[2, 2] = "80";
        xlWorkSheet.Cells[2, 3] = "65";
        xlWorkSheet.Cells[2, 4] = "45";

        xlWorkSheet.Cells[3, 1] = "Term2";
        xlWorkSheet.Cells[3, 2] = "78";
        xlWorkSheet.Cells[3, 3] = "72";
        xlWorkSheet.Cells[3, 4] = "60";

        xlWorkSheet.Cells[4, 1] = "Term3";
        xlWorkSheet.Cells[4, 2] = "82";
        xlWorkSheet.Cells[4, 3] = "80";
        xlWorkSheet.Cells[4, 4] = "65";

        xlWorkSheet.Cells[5, 1] = "Term4";
        xlWorkSheet.Cells[5, 2] = "75";
        xlWorkSheet.Cells[5, 3] = "82";
        xlWorkSheet.Cells[5, 4] = "68";

        xlWorkSheet.Cells[1, 2].Font.Bold = true;
        xlWorkSheet.Cells[1, 3].Font.Bold = true;
        xlWorkSheet.Cells[1, 4].Font.Bold = true;

        Excel.Range chartRange;

        Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
        Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 300, 250);
        Excel.Chart chartPage = myChart.Chart;

        chartRange = xlWorkSheet.get_Range("A1", "d5");
        chartPage.SetSourceData(chartRange, misValue);
        chartPage.ChartType = Excel.XlChartType.xlColumnClustered;


        xlWorkBook.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF, "z:\\Brisi\\ExcelToPdf.pdf");
        xlWorkBook.Close(true, misValue, misValue);
        xlApp.Quit();

非常感谢@Yaman:)非常欢迎@alenan2016