programing

PDF C# 라이브러리로 Excel 전송

closeapi 2023. 9. 3. 16:18
반응형

PDF C# 라이브러리로 Excel 전송

PDF 변환기/라이브러리 또는 API로 변환할 MsExcel(.xsl 및 .xlsx)을 찾고 있습니다.제 C#으로 주세요.넷 애플리케이션.

저는 상업 도서관을 좋아하지만, 많은 돈을 낼 수 없습니다.

저는 Interop을 통한 직접적인 COM 상호 작용에서 벗어나 타사 패키지를 통해 COM 상호 작용을 시도해 보았지만, 비용 문제로 인해 이를 해결하기 위해 Office 2007/2010의 내장 내보내기 기능을 사용할 것입니다.

호출해야 하는 방법은 워크북입니다.고정 형식으로 내보내기()

내보내기 기능으로 사용하는 방법의 예는 다음과 같습니다.

public bool ExportWorkbookToPdf(string workbookPath, string outputPath)
{
    // If either required string is null or empty, stop and bail out
    if (string.IsNullOrEmpty(workbookPath) || string.IsNullOrEmpty(outputPath))
    {
        return false;
    }

    // Create COM Objects
    Microsoft.Office.Interop.Excel.Application excelApplication;
    Microsoft.Office.Interop.Excel.Workbook excelWorkbook;

    // Create new instance of Excel
    excelApplication = new Microsoft.Office.Interop.Excel.Application();

    // Make the process invisible to the user
    excelApplication.ScreenUpdating = false;

    // Make the process silent
    excelApplication.DisplayAlerts = false;

    // Open the workbook that you wish to export to PDF
    excelWorkbook = excelApplication.Workbooks.Open(workbookPath);

    // If the workbook failed to open, stop, clean up, and bail out
    if (excelWorkbook == null)
    {
        excelApplication.Quit();

        excelApplication = null;
        excelWorkbook = null;

        return false;
    }

    var exportSuccessful = true;
    try
    {
        // Call Excel's native export function (valid in Office 2007 and Office 2010, AFAIK)
        excelWorkbook.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, outputPath);
    }
    catch (System.Exception ex)
    {
        // Mark the export as failed for the return value...
        exportSuccessful = false;

        // Do something with any exceptions here, if you wish...
        // MessageBox.Show...        
    }
    finally
    {
        // Close the workbook, quit the Excel, and clean up regardless of the results...
        excelWorkbook.Close();
        excelApplication.Quit();

        excelApplication = null;
        excelWorkbook = null;
    }

    // You can use the following method to automatically open the PDF after export if you wish
    // Make sure that the file actually exists first...
    if (System.IO.File.Exists(outputPath))
    {
        System.Diagnostics.Process.Start(outputPath);
    }

    return exportSuccessful;
}

이 기사들은 당신에게 도움이 될 것입니다!

PDF 변환기 서비스

iTextSharp

PDF .NET으로 Excel 전송

편집: 이 클래스 함수를 찾았습니다.

public DataSet GetExcel(string fileName)
    {
        Application oXL;
        Workbook oWB;
        Worksheet oSheet;
        Range oRng;
        try 
        {
            //  creat a Application object
            oXL = new ApplicationClass();
            //   get   WorkBook  object
            oWB = oXL.Workbooks.Open(fileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                    Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                    Missing.Value, Missing.Value);

            //   get   WorkSheet object 
            oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.Sheets[1];
            System.Data.DataTable dt = new System.Data.DataTable("dtExcel");
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            DataRow dr;

            StringBuilder sb = new StringBuilder();
            int jValue = oSheet.UsedRange.Cells.Columns.Count;
            int iValue = oSheet.UsedRange.Cells.Rows.Count;
            //  get data columns
            for (int j = 1; j <= jValue; j++)
            {
                dt.Columns.Add("column" + j, System.Type.GetType("System.String"));
            }

            //string colString = sb.ToString().Trim();
            //string[] colArray = colString.Split(':');

            //  get data in cell
            for (int i = 1; i <= iValue; i++)
            {
                dr = ds.Tables["dtExcel"].NewRow();
                for (int j = 1; j <= jValue; j++)
                {
                    oRng = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[i, j];
                    string strValue = oRng.Text.ToString();
                    dr["column" + j] = strValue;
                }
                ds.Tables["dtExcel"].Rows.Add(dr);
            }
            return ds;
        }
        catch (Exception ex)
        {
            Label1.Text = "Error: ";
            Label1.Text += ex.Message.ToString();
            return null;
        }
        finally
        {
            Dispose();
        } 

편집 2: 또한 이 기사가 당신에게 도움이 된다는 것을 알았습니다!

http://www.c-sharpcorner.com/UploadFile/psingh/PDFFileGenerator12062005235236PM/PDFFileGenerator.aspx

다음을 시도합니다.

http://www.sautinsoft.com/convert-excel-xls-to-pdf/spreadsheet-xls-excel-to-pdf-export-component-asp.net.php

또는

http://www.html-to-pdf.net/excel-library.aspx

이를 위해 IT 텍스트를 조작할 수도 있다고 생각합니다. http://www.itextpdf.com/

http://www.aspose.com 도 있지만 특별히 저렴하지는 않습니다.

스택 오버플로에 대한 다음 답변이 도움이 될 수 있습니다.https://stackoverflow.com/questions/891531/convert-xls-doc-files-to-pdf-with-c https://stackoverflow.com/questions/769246/xls-to-pdf-conversion-inside-net .두 번째 답변은 오픈 오피스를 자동화하는 흥미로운 솔루션입니다!!

언급URL : https://stackoverflow.com/questions/5499562/excel-to-pdf-c-sharp-library

반응형