首页上一页 1 下一页尾页 1 条记录 1/1页
c#典型模块精讲,导出到excel表中,貌似得不到结果
发表在C#图书答疑
2013-01-12
是否精华
是
否
版块置顶:
是
否
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Microsoft.Office.Interop.Excel;
using System.IO;
using Office = Microsoft.Office.Core;
using System.Resources;
namespace frmdataexport
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
groupBox1.Text = "数据表名称:";
string selectSQL = "select * from [教务管理系统].[dbo].[学生信息] left join[教务管理系统].[dbo].[课程信息] on [教务管理系统].[dbo].[学生信息].[班级编号]=[教务管理系统].[dbo].[课程信息] .[班级编号]";
string connectionstring = "Data Source=HU-PC\\HULINLIN;Initial Catalog=教务管理系统;User ID=sa;Password=123456";
SqlConnection sqlconnection = new SqlConnection(connectionstring);
SqlDataAdapter adapter = new SqlDataAdapter(selectSQL, sqlconnection);
sqlconnection.Open();
DataSet ds = new DataSet();
adapter.Fill(ds);
if (ds.Tables[0].Rows.Count != 0)
{
dataGridView1.DataSource = ds.Tables[0].DefaultView;
}
else
dataGridView1.DataSource = null;
sqlconnection.Close(); ;
}
private void exportdata_Click(object sender, EventArgs e)
{public void exportdata1(DataGridView sredgv, string filename)
{ string type = filename.Substring(filename.IndexOf(".")+1);
if(type.Equals("xls",StringComparison.CurrentCultureIgnoreCase))
{ Excel.application excel = new Excel.application();
try
{ excel.displayalter = false;
excel.workbooks.add(true);
excel.visible = false;
for(int i=0;i<dataGridView1.Columns.Count;i++)
{excel.cell[2,i+1]=dataGridView1.Columns[i].HeaderText;}
for(int i=0;i<dataGridView1.Rows.Count;i++)
{ for(int j=0;j<dataGridView1.Columns.Count;j++)
excel.cell[i+19][j+1]=dataGridView1[j,i].Value;}
excel.workbooks[1].savecopyas(filename);}
finally
{excel.quit();}
return;}
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Microsoft.Office.Interop.Excel;
using System.IO;
using Office = Microsoft.Office.Core;
using System.Resources;
namespace frmdataexport
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
groupBox1.Text = "数据表名称:";
string selectSQL = "select * from [教务管理系统].[dbo].[学生信息] left join[教务管理系统].[dbo].[课程信息] on [教务管理系统].[dbo].[学生信息].[班级编号]=[教务管理系统].[dbo].[课程信息] .[班级编号]";
string connectionstring = "Data Source=HU-PC\\HULINLIN;Initial Catalog=教务管理系统;User ID=sa;Password=123456";
SqlConnection sqlconnection = new SqlConnection(connectionstring);
SqlDataAdapter adapter = new SqlDataAdapter(selectSQL, sqlconnection);
sqlconnection.Open();
DataSet ds = new DataSet();
adapter.Fill(ds);
if (ds.Tables[0].Rows.Count != 0)
{
dataGridView1.DataSource = ds.Tables[0].DefaultView;
}
else
dataGridView1.DataSource = null;
sqlconnection.Close(); ;
}
private void exportdata_Click(object sender, EventArgs e)
{public void exportdata1(DataGridView sredgv, string filename)
{ string type = filename.Substring(filename.IndexOf(".")+1);
if(type.Equals("xls",StringComparison.CurrentCultureIgnoreCase))
{ Excel.application excel = new Excel.application();
try
{ excel.displayalter = false;
excel.workbooks.add(true);
excel.visible = false;
for(int i=0;i<dataGridView1.Columns.Count;i++)
{excel.cell[2,i+1]=dataGridView1.Columns[i].HeaderText;}
for(int i=0;i<dataGridView1.Rows.Count;i++)
{ for(int j=0;j<dataGridView1.Columns.Count;j++)
excel.cell[i+19][j+1]=dataGridView1[j,i].Value;}
excel.workbooks[1].savecopyas(filename);}
finally
{excel.quit();}
return;}
}
}
}
}