如何使用 Excel 互操作保存工作簿而不显示保存对话框?
我必须创建一个控制台应用程序,将 DataSet
导出到 Excel。问题是它不应该弹出保存窗口,它应该自动创建 Excel 文件。到目前为止我有以下代码,但我不知道如何使其自动保存。将不胜感激任何帮助。
public static void CreateWorkbook(DataSet ds, String path)
{
int rowindex = 0;
int columnindex = 0;
Microsoft.Office.Interop.Excel.Application wapp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Worksheet wsheet;
Microsoft.Office.Interop.Excel.Workbook wbook;
wapp.Visible = false;
wbook = wapp.Workbooks.Add(true);
wsheet = (Worksheet)wbook.ActiveSheet;
try
{
for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
{
wsheet.Cells[1, i + 1] = ds.Tables[0].Columns[i].ColumnName;
}
foreach (DataRow row in ds.Tables[0].Rows)
{
rowindex++;
columnindex = 0;
foreach (DataColumn col in ds.Tables[0].Columns)
{
columnindex++;
wsheet.Cells[rowindex + 1, columnindex] = row[col.ColumnName];
}
}
}
catch (Exception ex)
{
String err = ex.Message;
}
wapp.UserControl = true;
}
I have to create a Console application that exports a DataSet
to Excel. The problem is that it shouldn't pop up the save window, it should automatically create the Excel file. So far I have the following code, but I don't know how to make it save automatically. Would appreciate any help.
public static void CreateWorkbook(DataSet ds, String path)
{
int rowindex = 0;
int columnindex = 0;
Microsoft.Office.Interop.Excel.Application wapp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Worksheet wsheet;
Microsoft.Office.Interop.Excel.Workbook wbook;
wapp.Visible = false;
wbook = wapp.Workbooks.Add(true);
wsheet = (Worksheet)wbook.ActiveSheet;
try
{
for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
{
wsheet.Cells[1, i + 1] = ds.Tables[0].Columns[i].ColumnName;
}
foreach (DataRow row in ds.Tables[0].Rows)
{
rowindex++;
columnindex = 0;
foreach (DataColumn col in ds.Tables[0].Columns)
{
columnindex++;
wsheet.Cells[rowindex + 1, columnindex] = row[col.ColumnName];
}
}
}
catch (Exception ex)
{
String err = ex.Message;
}
wapp.UserControl = true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
WorkBook.SaveAs()
的所有参数都是可选的,但如果您愿意,您可以对大多数参数使用Type.Missing
。典型的调用如下所示:
请注意,我没有包含文件扩展名; Excel 将为您设置。
工作簿。另存为方法 (Microsoft.Office.Tools.Excel) | Microsoft Docs 描述了每个参数。
All of the arguments to
WorkBook.SaveAs()
are optional, but you can just useType.Missing
for most of them if you want to.The typical call would look like:
Note that I didn't include the file extension; Excel will set that for you.
Workbook.SaveAs Method (Microsoft.Office.Tools.Excel) | Microsoft Docs describes each of the arguments.
尝试调用工作簿的
SaveAs
方法。对于大量参数,请尝试将Type.Missing
传递给除第一个参数(文件名)之外的所有参数。Try to call the
SaveAs
method of the workbook. For the lot of parameter, try to passType.Missing
to all parameters but the first ( the file name ).将
ConflictResolution
添加到XlSaveConflictResolution.xlLocalSessionChanges
并将应用程序的
DisplayAlerts
属性设置为false
,这样窗口就不会显示。请参阅 XlSaveConflictResolution 枚举 (Microsoft.Office.Interop. Excel)| Microsoft 文档 了解更多信息。
Add
ConflictResolution
toXlSaveConflictResolution.xlLocalSessionChanges
and set the application's
DisplayAlerts
property tofalse
so the window won't show.see XlSaveConflictResolution Enum (Microsoft.Office.Interop.Excel) | Microsoft Docs for more info.
请参阅 MSDN 文档
See MSDN Documentation
将
Application.DisplayAlerts
属性设置为false
将停止显示其所有工作簿的所有警报。setting
Application.DisplayAlerts
property tofalse
will stop displaying all alerts for all of its workbooks.使用
open xml sdk
生成文档而不是自动化;它更可靠。Use
open xml sdk
to generate documents instead of automation; it is much more reliable.使用 DisplayAlerts 属性。它非常简单和快速。
当您要保存文件时,切勿让 Excel 定义文件类型。因为格式可能会改变。
如果原始文档是.xls,并且当前用户使用的是office 2013,默认格式是.xlsx,然后Excel会将文件转换为.xlsx,没有Excel 2010或更高版本的用户无法打开该文件。
Use the DisplayAlerts property. Its very simple and fast.
Never let the Excel define the kind of file when you're going to save the file. Because it's possible that the format will be changed.
If original doc was .xls and the current user has the office 2013 the default format its .xlsx and then the Excel will convert the file to .xlsx and the users tha doesnt have the Excel 2010 or superior cant open the file.