在Vs2005 C#中有选择的导出Excel报表(单行)

在Vs2005 C#中有选择的导出Excel报表(单行)

首先引入命名空间E:/Program Files/Microsoft Office/OFFICE11/EXCEL.EXE

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;
using System.Xml;


namespace WindowsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

      
        private DataSet ds = new DataSet();
      

        private void Form2_Load(object sender, EventArgs e)
        {
            string constr = "server=xfj;database=HanBan;uid=sa;pwd=ok;";
            string sql = "select * from PersonalTax";
            SqlConnection con = new SqlConnection(constr);
            try
            {
                con.Open();
                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(sql, con);
                da.Fill(ds, "PersonalTax");
                da.Dispose();
                dataGridView1.DataSource = ds.Tables[0].DefaultView;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
            }
        }

        //导出Excel报表
        private void btn_Pint_Click(object sender, EventArgs e)
        {
            string sql = "select * from PersonalTax";
            string constr = "server=xfj;database=HanBan;uid=sa;pwd=ok;";
            SqlConnection con = new SqlConnection(constr);
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            string pathName = System.Windows.Forms.Application.StartupPath.Trim();
            string dateTime = dateTimePicker1.Text.Split(char.Parse("-"))[0] + "年" + dateTimePicker1.Text.Split(char.Parse("-"))[1] + "月";
            int columnCount = 0;

            FileInfo mode = new FileInfo(pathName + "//Personal.xls");
            try
            {
                mode.CopyTo(pathName +"//" + dateTime + " Personal.xls", true);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            object missing = Type.Missing;
            Excel.Application myExcel = new Excel.Application();
            myExcel.Application.Workbooks.Open(pathName + dateTime + " Personal.xls", missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
            myExcel.Visible = false;

            Excel.Workbook myBook = myExcel.Workbooks[1];
            Excel.Worksheet mySheet = (Excel.Worksheet)myBook.Worksheets[1];

           
            try
            {
                if (ds.Tables.Contains("PersonalTax"))
                {
                    ds.Tables["PersonalTax"].Clear();
                }
                da.Fill(ds, "PersonalTax");
                columnCount = ds.Tables["PersonalTax"].Rows.Count;
                for (int i = 0; i < columnCount; i++)
                {
                    ds.Tables["PersonalTax"].Rows[i][6] = dateTime;
                }
                dataGridView1.DataSource = ds.Tables["PersonalTax"];

                if (columnCount == 0)
                {
                    MessageBox.Show("没有可打印的数据!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    myExcel.Cells[6,2] = dateTime;

                    for (int i = 0; i < columnCount/2; i++)
                    {
                        ((Excel.Range)mySheet.Cells[i + 6, 1]).EntireRow.Insert(0, Excel.XlDirection.xlDown);  //添加行
                        //(Excel.Range)mySheet.Rows[i + 6, 1].Insert(Excel.XlDirection.xlDown);

                        mySheet.Cells[i + 6, 1] = ds.Tables["PersonalTax"].Rows[i * 2 + 0][0].ToString();
                        mySheet.Cells[i + 6, 2] = ds.Tables["PersonalTax"].Rows[i * 2 + 0][1].ToString();
                        mySheet.Cells[i + 6, 3] = ds.Tables["PersonalTax"].Rows[i * 2 + 0][2].ToString();

                        mySheet.Cells[i + 6, 4] = ds.Tables["PersonalTax"].Rows[i * 2 + 1][0].ToString();
                        mySheet.Cells[i + 6, 5] = ds.Tables["PersonalTax"].Rows[i * 2 + 1][1].ToString();
                        mySheet.Cells[i + 6, 6] = ds.Tables["PersonalTax"].Rows[i * 2 + 1][2].ToString();
                    }
                    if (columnCount % 2 == 1)
                    {
                        ((Excel.Range)myExcel.Cells[columnCount / 2 + 6, 1]).EntireRow.Insert(0, Excel.XlDirection.xlDown);  //添加行

                        mySheet.Cells[columnCount / 2 + 6, 1] = ds.Tables["PersonalTax"].Rows[columnCount - 1][0].ToString();
                        mySheet.Cells[columnCount / 2 + 6, 2] = ds.Tables["PersonalTax"].Rows[columnCount - 1][1].ToString();
                        mySheet.Cells[columnCount / 2 + 6, 3] = ds.Tables["PersonalTax"].Rows[columnCount - 1][3].ToString();
                    }
                }

                if (columnCount % 2 == 1)
                {
                    columnCount = columnCount % 2 + 1;
                }
                else
                {
                    columnCount = columnCount % 2;
                }
                Excel.Range r = mySheet.get_Range(mySheet.Cells[6, 1], mySheet.Cells[5 + columnCount, 5]);
                r.Select();
                myExcel.Run("Macro1", missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
                myBook.Save();
                myExcel.Visible = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                if (myBook != null)
                {
                    myBook.Close(false, missing, missing);
                }
                myExcel.Workbooks.Close();
                myExcel.Quit();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);
            }
            finally
            {
                da.Dispose();
            }
        }

                 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值