I have a Excel workbook that allows me to input the values of some fields, click on a button, and then a PDF report it's generated based on my input. I then have to choose the directory where to save this PDF file.
我有一个Excel工作簿,允许我输入一些字段的值,单击一个按钮,然后根据我的输入生成一个PDF报告。然后我必须选择保存此PDF文件的目录。
I have to create an REST-like API that will receive a payload with the input parameters for the Excel worksheet, and return the generated PDF file.
我必须创建一个类似REST的API,它将接收带有Excel工作表输入参数的有效负载,并返回生成的PDF文件。
It isn't possible for me to have access to the macros, data and logic inside of the Workbook, so I can't just implement the same logic directly in my API. The Workbook will also change periodically (formulas, macros, etc), and the only thing that will remain is the input fields and the PDF output.
我无法访问工作簿中的宏,数据和逻辑,所以我不能直接在我的API中实现相同的逻辑。工作簿也会定期更改(公式,宏等),唯一剩下的就是输入字段和PDF输出。
After some tests with this library, I'm not sure if it allows me to call a button (to generate the PDF) and specify the output directory. I mainly use C# and .NET, how can I achieve this?
在使用此库进行一些测试后,我不确定它是否允许我调用按钮(生成PDF)并指定输出目录。我主要使用C#和.NET,我该如何实现?
1 个解决方案
#1
0
Have you considered using the Excel Interop library? You can get it through Nuget by installing the Microsoft.Office.Interop.Excel
package.
您是否考虑过使用Excel Interop库?您可以通过安装Microsoft.Office.Interop.Excel包来通过Nuget获取它。
That will allow you to open up the workbook, write some values into some cells, and run the macro which is called by the button.
这将允许您打开工作簿,将一些值写入某些单元格,然后运行按钮调用的宏。
// Start a new instance of Excel
var app = new Interop.Application();
// Open the workbook
var workbook = app.Workbooks.Open(path);
// Set some values
workbook.Worksheets["Sheet1"].Range["A1"].Value2 = 123;
// Run the macro
app.Run("Module1.MyMacro");
There are some limitations to this though. The computer which you run this on must have Excel installed, since the Interop library is literally opening Excel. Also, the Application.Run
method will return instantly, before the macro has actually finished running. You could possibly get around this by continually checking for the PDF output file to appear:
但是这有一些限制。您运行此计算机的计算机必须安装Excel,因为Interop库实际上是打开Excel。此外,Application.Run方法将在宏实际运行之前立即返回。您可以通过不断检查PDF输出文件来显示:
while (!File.Exists(path))
{
Thread.Sleep(5000); // wait for 5 seconds
}
#1
0
Have you considered using the Excel Interop library? You can get it through Nuget by installing the Microsoft.Office.Interop.Excel
package.
您是否考虑过使用Excel Interop库?您可以通过安装Microsoft.Office.Interop.Excel包来通过Nuget获取它。
That will allow you to open up the workbook, write some values into some cells, and run the macro which is called by the button.
这将允许您打开工作簿,将一些值写入某些单元格,然后运行按钮调用的宏。
// Start a new instance of Excel
var app = new Interop.Application();
// Open the workbook
var workbook = app.Workbooks.Open(path);
// Set some values
workbook.Worksheets["Sheet1"].Range["A1"].Value2 = 123;
// Run the macro
app.Run("Module1.MyMacro");
There are some limitations to this though. The computer which you run this on must have Excel installed, since the Interop library is literally opening Excel. Also, the Application.Run
method will return instantly, before the macro has actually finished running. You could possibly get around this by continually checking for the PDF output file to appear:
但是这有一些限制。您运行此计算机的计算机必须安装Excel,因为Interop库实际上是打开Excel。此外,Application.Run方法将在宏实际运行之前立即返回。您可以通过不断检查PDF输出文件来显示:
while (!File.Exists(path))
{
Thread.Sleep(5000); // wait for 5 seconds
}