VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#教程之C# 将数据写入EXCEL表中

需要添加引用  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进程  
 

}    
复制代码

 

有兴趣的可以看看 https://www.cnblogs.com/junshijie/p/5292087.html 这篇文章,里面有更详细如何操作EXCEL

 

 
分类: C#
 
 
 
« 上一篇:CRC16位校验
» 下一篇:简述System.Windows.Forms.Timer 与System.Timers.Timer用法区别
posted on 2018-01-22 15:08 永恒921 阅读(1696) 评论(2) 编辑 收藏
 

FeedBack:
 
#1楼  2018-01-23 16:20 cnc  
不建议楼主使用该Microsoft.Office.Interop.Excel组件,因为需要安装Excel,
建议使用GemBox.Spreadsheet.dll
具体文章为:
http://www.cnblogs.com/cncc/p/7992383.html
支持(0)反对(0)
  
#2楼[楼主]  2018-01-24 08:55 永恒921  
@ cnc
引用不建议楼主使用该Microsoft.Office.Interop.Excel组件,因为需要安装Excel,
建议使用GemBox.Spreadsheet.dll
恩,确实很好。


相关教程