asp.net - 如何保存由 Microsoft.Office.Interop.Excel 生成的文件
问题描述
我正在使用 Microsoft Interop.Excel 生成一个 excel 文件。我需要您选择保存位置。我在做 ASP.NET C#。
这是我到目前为止所拥有的:
Application excel = new Application();
Workbook wb = excel.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Worksheet ws = (Worksheet)wb.Worksheets.get_Item(1);
ws.Name = "Nome da Pasta";
ws.Cells[1, 1] = "POÇO:";
ws.Cells[2, 1] = "CAMPO:";
ws.Cells[3, 1] = "PERIODO:";
ws.Cells[5, 1] = "DATA HORA (M/D/Y)";
ws.Cells[5, 2] = "PRESSÃO DO POÇO (PSI)";
ws.Cells[5, 3] = "RPM DO POÇO";
ws.Columns.AutoFit();
excel.Quit();
解决方案
在您的代码示例中,将其更改如下:
try {
Application excel = new Application();
Workbook wb = excel.Workbooks.Add (XlWBATemplate.xlWBATWorksheet);
Worksheet ws = (Worksheet)wb.Worksheets.get_Item(1);
ws.Name = "Nome da Pasta";
ws.Cells[1, 1] = "POÇO:";
ws.Cells[2, 1] = "CAMPO:";
ws.Cells[3, 1] = "PERIODO:";
ws.Cells[5, 1] = "DATA HORA (M/D/Y)";
ws.Cells[5, 2] = "PRESSÃO DO POÇO (PSI)";
ws.Cells[5, 3] = "RPM DO POÇO";
ws.Columns.AutoFit();
//Save your file
wb.SaveAs("c:\temp\SaveExample.xlsx");
excel.Quit();
} catch (Exception ex) {
//Log the exception, to NLog, or a file or something.
//Otherwise, don't use a try/catch. Any error would get
//logged in the Server's NT Event Log (under Application).
//If this code doesn't work. Look there for the problem and
//callstack.
}
最后一件事:您的保存路径必须可由 IIS 使用的同一帐户写入。最简单的(对于我上面的示例)是使C:\Temp\
文件夹为Everyone
.
推荐阅读
- spring - 百里香th:javascript中的id值
- python - Spacy BILOU 格式转 spacy json 格式
- python - BERT 总是预测同一个类(微调)
- python - 访问 StringProperty 数据并使用它 (KIVY)
- node.js - Nodejs Express + Nuxt - 会话 Cookie
- java - 如何使用android studio了解部分代码的性能
- sql - 有什么方法可以使用 IN 和 COUNT 重写此查询?
- google-api - 将范围限制为 Google 服务帐户的 Google Auth JWT 客户端中的单个文件
- sql - 如何将列数据透视到不能超过最大总数的行?
- java - Problem with: "Variable used as a try-with-resources resource should be final or effectively final"