EXCEL的互相转换 和打印功能

 个人觉得博客园还是很好的!

谢谢!

 代码连接如下:

 

http://www.cnblogs.com/suyangbin/archive/2011/11/29/2266938.html

 

把access数据库表导出到Excel表制定的位置

 

//创建Excel对象
            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
            excel.Application.Workbooks.Add(true);

            //设置Excel标题
            excel.Caption = "学生成绩";

            //设置Excel列名
            excel.Cells[1, 1] = "学生姓名";
            excel.Cells[1, 2] = "科目";
            excel.Cells[1, 3] = "成绩";
            excel.Cells[1, 4] = "考试时间";

            //设置Excel字体加粗
            excel.get_Range(excel.Cells[1, 1], excel.Cells[1, 4]).Font.Bold = true;
            //设置Excel字体颜色
            excel.get_Range(excel.Cells[1, 1], excel.Cells[1, 4]).Font.ColorIndex = 0;
            //设置Excel边框样式
            excel.get_Range(excel.Cells[1, 1], excel.Cells[1, 4]).Borders.LineStyle = XlLineStyle.xlContinuous;

            //循环将DataGridView中的数据赋值到Excel中
            for (int m = 0; m < dgvResult.Rows.Count; m++)
            {
                DataGridViewComboBoxCell dgvCbo = (DataGridViewComboBoxCell)dgvResult.Rows[m].Cells["StudentNo"];
                excel.Cells[m + 2, 1] = dgvCbo.FormattedValue.ToString();//设置学生姓名
            }
            int i;
            for (i = 0; i < dgvResult.Rows.Count; i++)
            {
                excel.Cells[i + 2, 2] = dgvResult.Rows[i].Cells["SubjectName"].Value.ToString();
                excel.Cells[i + 2, 3] = dgvResult.Rows[i].Cells["StudentResult"].Value.ToString();
                excel.Cells[i + 2, 4] = dgvResult.Rows[i].Cells["ExamDate"].Value.ToString();
            }
            //设置Excel水平对齐方式
            excel.get_Range(excel.Cells[1, 1], excel.Cells[i + 2, 4]).HorizontalAlignment = XlHAlign.xlHAlignLeft;

            //显示当前窗口
            excel.Visible = true;
 
 
 private void ConfigureCrystalReports()

    {
       
        //连接字串
        String connstr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\CrZen\testall.mdb;";
        OleDbDataAdapter da = new OleDbDataAdapter();
        OleDbConnection cn = new OleDbConnection(connstr);
        //
        da = new OleDbDataAdapter("SELECT * From RPT_CR_TEST1", cn);
        //创建我们的DataSet1实例
        DataSet1 dt1 = new DataSet1();
        //填充dt1
        //注意:表名mytable必须与我们在xsd设计的表名称一致。
        //本例中数据库的表实际名称为RPT_CR_TEST1,而最终是以mytable为准的
        //使用 PUSH模式的优点就在此,可以自由组合SQL
        //前提是表名称和字段名(需要在SQL中使用as别名的方式跟xsd中设计的字段名一致)都要一致
        da.Fill(dt1, "mytable"); 
         
        ReportDocument myReport = new ReportDocument();
        string reportPath = Server.MapPath("crystalreport1.rpt");
        myReport.Load(reportPath);

        //绑定数据集,注意,一个报表用一个数据集。
        myReport.SetDataSource(dt1);
        CrystalReportViewer1.ReportSource = myReport;

    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值