当前位置:编程学习 > C#/ASP.NET >>

新人求助将c#datagridview数据导入microsoft excel 12.0中

本人新手,所以很多问题无法解决,搜索网上很多相关代码,没有一个可以运行,希望哪位高手能帮帮忙,感激不尽!
我用的是VS2010,数据库SQL2008,excel版本为2007.
希望能直接给出将c#datagridview数据导入microsoft excel 的代码,,尤其是需要using System的相关指引代码,谢谢! excel 数据库 datagridview --------------------编程问答-------------------- C#datagridview和excel数据的相互导入方法

--------------------编程问答-------------------- http://blog.csdn.net/xianfajushi/article/details/8093957 --------------------编程问答-------------------- 首先在“解决方案资源管理器”那里项目的引用那里右击》添加引用Microsoft.Office.Interop.Excel,然后再using Microsoft.Office.Interop.Excel;

代码:
//……
 string strSQL = "select * from dc_vote";
            SqlConnection conn = new SqlConnection(myData.conStrSQL);
            SqlCommand cmd = new SqlCommand(strSQL, conn);
            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = cmd;
            DataSet ds = new DataSet();
            sda.Fill(ds, "AA");

   Microsoft.Office.Interop.Excel.Application app =
                       new Microsoft.Office.Interop.Excel.ApplicationClass();

            app.Visible = false;
            Workbook wBook = app.Workbooks.Add(true);
            Worksheet wSheet = wBook.Worksheets[1] as Worksheet;
            wSheet.Cells[1, 1] = "教师辅导情况满意度调查结果统计";

            wSheet.Cells[2, 1] = "参评学生总数:";
            wSheet.Cells[2, 2] = ds.Tables[0].Rows[0][1].ToString();
//……

 int nowrow = 0;

            ((Range)wSheet.Cells[1, 1]).EntireColumn.ColumnWidth = 30;
            ((Range)wSheet.Cells[1, 4]).EntireColumn.ColumnWidth = 15;
            ((Range)wSheet.Cells[1, 6]).EntireColumn.ColumnWidth = 20;
            ((Range)wSheet.Cells[1, 6]).EntireColumn.WrapText = true;
            wSheet.get_Range(wSheet.Cells[1, 1], wSheet.Cells[1, 6]).MergeCells = true;//合并单元格:标题
            ((Range)wSheet.Cells[1, 1]).HorizontalAlignment = XlHAlign.xlHAlignCenter;     //设置字体在单元格内的对其方式
            wSheet.get_Range(wSheet.Cells[2, 1], wSheet.Cells[nowrow + 2, 6]).BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null);

            Microsoft.Office.Interop.Excel.Range range = (Range)wSheet.Cells[1, 1];
            range.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThin, XlColorIndex.xlColorIndexAutomatic, null);

            range.Interior.ColorIndex = 15;//背景颜色
            range.Font.Bold = true;//粗体
            range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;//居中
            //加边框
            range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null);

            progressBar1.Value = 100;

            app.Visible = true;


补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,