在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();
}
}
}