




已阅读5页,还剩1页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C#将数据保存到Excel中第一步:首先添加两个组件:打开解决方案资资源管理器,右键点击“引用”,选择“添加引用”添加第一个引用:再添加第二个引用:第二步:引用命名空间: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; /创建一个新的工作簿 Excel.Workbooks workbooks = excel.Workbooks; Excel.Workbook workbook = workbooks.Add(miss); Excel.Worksheet sheet = workbook.ActiveSheet; /循环添加列名 for (int i = 0; i this.dataGridView1.Rows0.Cells.Count; i+) excel.Cells1, i + 1 = this.dataGridView1.Columnsi.HeaderText.ToString(); /填充数据 for (int i = 0; i this.dataGridView1.Rows.Count; i+)/所要添加的行数 for (int j = 0; j this.dataGridView1.Rows0.Cells.Count; j+)/每行的列数 /将数据填充到对应的单元格中 excel.Cellsi + 2, j + 1 = Convert.ToString(this.dataGridView1j, i.Value); /显示数据导出的进度 gressBar1.Value += 100 / this.dataGridView1.RowCount; /设置表格样式 /设置列标题的背景颜色 Excel.Range er = sheet.Rangesheet.Cells1, 1, sheet.Cells1, this.dataGridView1.ColumnCount; er.Cells.Interior.Color = Color.LightBlue; /重新选择单元格范围 int rowscount = this.dataGridView1.Rows.Count; int columncount = this.dataGridView1.ColumnCount; /将范围重新确定为每一行的第一个单元格 er = sheet.Rangesheet.Cells1, 1, sheet.Cellsrowscount, 1; /设置范围内的单元格的背景颜色为淡蓝色 er.Cells.Interior.Color = Color.LightBlue; /选中EXCEL所有表格 er = sheet.Rangesheet.Cells1, 1, sheet.Cellsrowscount, columncount; /让EXCEL中的所有单元格的列宽碎文字的长短自动调整 er.EntireColumn.AutoFit(); / 让EXCEL的文本水平居中方式 er.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter; /在表的结尾添加数据导出的时间 er = sheet.Rangesheet.Cellsrowscount + 1, 1, sheet.Cellsrowscount + 2, columncount; er.Merge(0); er.Value = 数据生成时间: + DateTime.Now; /设置单元格的背景颜色 /er.Cells.Interior.Color = Color.LightBlue; / 文本水平居中方式 er.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter; /将文字的颜色设为红蛇 er.Font.Color = Color.Red; /保存文件 sheet.SaveAs(this.saveFileDialog1.FileName, miss, miss, miss, miss, miss, Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss); /关闭表格 workbook.Close(
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 政审考试题库及答案解析
- 矿山电子高级考试题库及答案
- 征信考试题库及答案
- 商业合作市场调研分析报告合同
- 企业合同管理模板及风险提示
- 2025年新疆农作物制种质量保障合同
- 2025年贵州公需科目之乡村振兴试题(含答案)
- 祁阳历史中考试卷及答案
- 技校政治考试题目及答案
- 唐山单招十类考试题及答案
- 【百岁居】百岁居内外勤版本
- 国开(河北)2024年《商务谈判实务》形成性考核1-4答案
- 2024年上海交易集团有限公司招聘笔试冲刺题(带答案解析)
- 2024年首届全国“红旗杯”班组长大赛考试题库800题(含答案)
- 职场英语口语900句
- 物流地产行业报告:物流地产商业模式与投资解析
- 朝花夕拾鲁迅笔下的人物
- 尿常规和粪常规解读课件
- 设备类风险辨识培训课件
- DB32-T 4638-2024 智能泵站技术导则
- 黔菜菜名英译规范
评论
0/150
提交评论