本文整理汇总了C#中Microsoft.Office.Interop.Excel.Application.SaveWorkspace方法的典型用法代码示例。如果您正苦于以下问题:C# Microsoft.Office.Interop.Excel.Application.SaveWorkspace方法的具体用法?C# Microsoft.Office.Interop.Excel.Application.SaveWorkspace怎么用?C# Microsoft.Office.Interop.Excel.Application.SaveWorkspace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.Office.Interop.Excel.Application
的用法示例。
在下文中一共展示了Microsoft.Office.Interop.Excel.Application.SaveWorkspace方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SaveDataTableToExcel
public static void SaveDataTableToExcel(DataTable excelTable, string filePath)
{
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
try
{
app.Visible = false;
Microsoft.Office.Interop.Excel.Workbook wBook = app.Workbooks.Add(true);
Microsoft.Office.Interop.Excel.Worksheet wSheet = wBook.Worksheets[1] as Microsoft.Office.Interop.Excel.Worksheet;
if (excelTable.Rows.Count > 0)
{
int row = 0;
row = excelTable.Rows.Count;
int col = excelTable.Columns.Count;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
string str = excelTable.Rows[i][j].ToString();
wSheet.Cells[i + 2, j + 1] = str;
}
}
}
int size = excelTable.Columns.Count;
for (int i = 0; i < size; i++)
{
wSheet.Cells[1, 1 + i] = excelTable.Columns[i].ColumnName;
}
//设置禁止弹出保存和覆盖的询问提示框
app.DisplayAlerts = false;
app.AlertBeforeOverwriting = false;
//保存工作簿
wBook.Save();
//保存excel文件
app.Save(filePath);
app.SaveWorkspace(filePath);
app.Quit();
app = null;
}
catch
{
throw;
}
}