度量快速开发平台-专业、快速的软件定制快开平台
标题:
需要添加引用 Microsoft.Office.Interop.Excel
[打印本页]
作者:
Adam丶鱼
时间:
2020-7-1 18:08
标题:
需要添加引用 Microsoft.Office.Interop.Excel
private void WriteDataToExcel
{
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
DataTable dataTable1 = this.GetTabel1();//获取表格2
Microsoft.Office.Interop.Excel.Application excelApp;
Microsoft.Office.Interop.Excel._Workbook workBook;
Microsoft.Office.Interop.Excel._Worksheet workSheet;
Microsoft.Office.Interop.Excel._Worksheet workSheet1;
object misValue = System.Reflection.Missing.Value;
workBook = excelApp.Workbooks.Add(misValue);//加载模型
workSheet = (Microsoft.Office.Interop.Excel._Worksheet)workBook.Sheets.get_Item(1);//第一个工作薄。
workSheet1 = (Microsoft.Office.Interop.Excel._Worksheet)workBook.Sheets.get_Item(2);
int rowIndex = 0;
int colIndex = 0;
foreach (DataRow row in dataTable.Rows)
{
rowIndex++;
colIndex = 0;
foreach (DataColumn col in dataTable.Columns)
{
colIndex++;
workSheet.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString().Trim();
}
}
rowIndex = 0;
colIndex = 0;
foreach (DataRow row in dataTable1.Rows)
{
rowIndex++;
colIndex = 0;
foreach (DataColumn col in dataTable1.Columns)
{
colIndex++;
workSheet1.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString().Trim();
}
}
workSheet.Protect("MyPassword", Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, true, Type.Missing, Type.Missing);
//保护工作表
workSheet1.Protect("MyPassword", Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, true, Type.Missing, Type.Missing);
/**/
excelApp.Visible = false;
workBook.SaveAs(@"D:\outputFormDataBase1.xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue,
misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
misValue, misValue, misValue, misValue, misValue);
dataTable = null;
workBook.Close(true, misValue, misValue);
excelApp.Quit();
PublicMethod.Kill(excelApp);//调用kill当前excel进程
}
作者:
Adam丶鱼
时间:
2020-7-1 18:09
作者:
张兴康
时间:
2020-7-2 15:16
作者:
fteair
时间:
2020-7-2 17:26
欢迎光临 度量快速开发平台-专业、快速的软件定制快开平台 (http://plat.delit.cn/)
Powered by Discuz! X3.2