用c#创建一个库类

本文关键字:一个 创建 | 更新日期: 2023-09-27 18:01:33

我对此相当陌生,我希望在将方法转换为库类以用作dll时获得一些帮助。我的问题是我如何处理我正在使用的文本框值,以获得从用户传入的数据。

这是我的方法,我正在使用,并希望有一个可用的库类:

public Microsoft.Office.Interop.Excel.Workbook excelWorkbook { get; set; }
    void ExcelToPdf(string convertFilePath)
    {
        Microsoft.Office.Interop.Excel.Application appWord = new Microsoft.Office.Interop.Excel.Application();
        excelWorkbook = appWord.Workbooks.Open(DocumentUNCPath.Text);
        excelWorkbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, convertFilePath);
        excelWorkbook.Close();
        appWord.Quit();
    }

用c#创建一个库类

所以当你将它移动到库中时你需要将这些值注入到方法中:

public Microsoft.Office.Interop.Excel.Workbook excelWorkbook { get; set; }
void ExcelToPdf(string convertFilePath, string documentUncPath)
{
    Microsoft.Office.Interop.Excel.Application appWord = new Microsoft.Office.Interop.Excel.Application();
    excelWorkbook = appWord.Workbooks.Open(documentUncPath); // WAS DocumentUNCPath.Text
    excelWorkbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, convertFilePath);
    excelWorkbook.Close();
    appWord.Quit();
}

所以当你调用这个方法的时候你会从表单中传入DocumentUNCPath.Text的值

如果我理解正确的话。

在Visual Studio中选择,文件-新建-项目-类库。将代码复制并粘贴到类中,看起来应该像这样:

namespace ClassLibrary1
{
    public class Class1
    {
        public Microsoft.Office.Interop.Excel.Workbook excelWorkbook { get; set; }
        void ExcelToPdf(string convertFilePath)
        {
            Microsoft.Office.Interop.Excel.Application appWord = new Microsoft.Office.Interop.Excel.Application();
            excelWorkbook = appWord.Workbooks.Open(DocumentUNCPath.Text);
            excelWorkbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, convertFilePath);
            excelWorkbook.Close();
            appWord.Quit();
        }
    }
}

构建应用程序,你会发现你的dll在输出bin文件夹

相关文章: