C#将数据保存到Ex cel 中 第一步:首先添加两个组件: 打开解决方案资资源管理器,右键点击“引用”,选择“添加引用” 添加第一个引用: 再添加第二个引用: 第二步:引用命名空间: using System.Reflection;//这个命名空间是定义缺省值 第三步:界面设计(参考): 第四步:代码示例(仅供参考): /* 代码总有不完善之处,仅供参考 如有错误,敬请谅解 */ 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 System.Reflection; using System.Runtime.InteropServices; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //在窗体加载的时候当定数据 this.dataGridView1.DataSource = sqlconnection(); } //连接数据库获取相关数据 public DataTable sqlconnection() { //字段拼接 string str = "server = .;database =数据库名 ;integrated security = true"; SqlConnection conn = new SqlConnection(str); SqlCommand com = new SqlCommand(); com.Connection = conn; com.CommandText = "连接命令"; //打开连接 conn.Open(); SqlDataAdapter sda = new SqlDataAdapter(com); DataTable table = new DataTable(); //填充数据 sda.Fill(table); //关闭连接 conn.Close(); //返回数据 return table; } private void button1_Click(object sender, EventArgs e) { //判断将要导出的素具是否为空(不包括列标题) if (this.dataGridView1.Rows.Count <= 1) { MessageBox.Show("没有可导出的数据!"); return; } //选择保存路径 if (this.saveFileDialog1.ShowDialog() != DialogResult.OK) { return; } //创建一个 EXCEL 应用程序 Excel.Application excel = new Excel.Application(); //是否显示导出过程(显示创建后的EXCEL) excel.Visible = false; //定义缺省值 Missing miss = Missing.Value; //创...