Excel的互操作:保存工作簿不显示保存对话框 [英] Excel interop: saving workbook without showing save dialog
问题描述
我要创建出口数据集到Excel控制台应用程序。我尝试过谷歌搜索和寻找关于这个主题的职位,但还没有真正找到我要找的。的问题是,它不应该弹出保存窗口,它应自动创建Excel文件。到目前为止,我有以下的code,但我不知道如何使它自动保存。请问AP preciate任何帮助。
公共静态无效CreateWorkbook(DataSet的DS,字符串路径)
{
INT rowIndex位置= 0;
INT参数:columnIndex = 0;
Microsoft.Office.Interop.Excel.Application WAPP =新Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Worksheet wsheet;
Microsoft.Office.Interop.Excel.Workbook wbook;
wapp.Visible = FALSE;
wbook = wapp.Workbooks.Add(真正的);
wsheet =(表)wbook.ActiveSheet;
尝试
{
的for(int i = 0; I< ds.Tables [0] .Columns.Count;我++)
{
wsheet.Cells [1,I + 1] = ds.Tables [0] .Columns [I] .ColumnName;
}
的foreach(DataRow的行ds.Tables [0] .Rows)
{
rowIndex位置++;
参数:columnIndex = 0;
的foreach(COL的DataColumn在ds.Tables [0] .Columns)
{
参数:columnIndex ++;
wsheet.Cells [rowIndex位置+ 1,参数:columnIndex] =行[col.ColumnName]
}
}
}
赶上(例外前)
{
字符串ERR = ex.Message;
}
wapp.UserControl = TRUE;
}
所有的参数 WorkBook.SaveAs()
是可选的,但是你可以使用<$如果你想C> Type.Missing C $对于其中的大部分。
典型的通话将如下所示:
wbook.SaveAs(C:\\ \\温度等等,Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault,Type.Missing,Type.Missing,
假的,假的,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
wbook.Close();
请注意,我并没有包括文件扩展名,Excel将设置为你。
<一个href="http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.workbook.saveas(v=VS.100).aspx">http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.workbook.saveas(v=VS.100).aspx介绍了每个参数。
I have to create a console application that exports a dataset to Excel. I've tried googling and looking at posts on this topic but haven't really found what I'm looking for. 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;
}
All of the arguments to WorkBook.SaveAs()
are optional, but you can just use Type.Missing
for most of them if you want to.
The typical call would look like:
wbook.SaveAs("c:\\temp\\blah", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing,
false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
wbook.Close();
Note that I didn't include the file extension, Excel will set that for you.
http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.workbook.saveas(v=VS.100).aspx describes each of the arguments.
这篇关于Excel的互操作:保存工作簿不显示保存对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!