C#datagridview和excel数据的相互导入方法_第1页
C#datagridview和excel数据的相互导入方法_第2页
C#datagridview和excel数据的相互导入方法_第3页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、using System;using System.Data;using System.Data.OleDb;using System.IO;using System.Windows.Forms;using GemBox.ExcelLite;namespace WindowsFormsApplication5public partial class Form1 : Formpublic System.Data. DataTable biao = new System.Data. DataTable();public DataView dv = new DataView();public Sys

2、tem.Data. DataTable temp = new System.Data. DataTable();public Form1()InitializeComponent();shuchu();dataGridView1.DataSource = biao;/将表 bia o中的数据传递给 datagridview 显示。this.dataGridView1.Columns编号”.Frozen = true;public void shuchu()/System.Data.OleDb.OleDbConnection objConn = newSystem.Data.OleDb.OleD

3、bConnection(矿Provider=Microsoft.Jet.OLEDB.4.0;DataSource=excell.xls;Extended Properties=Excel 8.0;);System.Data.OleDb. OleDbConnection objConn = newSystem.Data.OleDb. OleDbConnection (Provider=Microsoft.Jet.OLEDB.4.0;DataSource=.excell.xls;Extended Properties=Excel 8.0;);DataSet ds = new DataSet ();

4、string TJ = SELECT * FROM myRange1$”;OleDbDataAdapter da = new OleDbDataAdapter(TJ, objConn);tryda.Fill(ds);catch (Exception ex)MessageBox.Show(ex.Message);objConn.Close();return;biao = ds.Tables0;/将excel表中的数据读入到内存表biao中,以备后用。 objConn.Close();/连接并打开数据库,将数据导入内存表biao中,myRangw1为excel中工作表名biao做查询使用。priv

5、ate void button1_Click(object sender, EventArgs e)ExcelFile excelFile = new ExcelFile();ExcelWorksheet sheet = excelFile.Worksheets.Add(Users);/定义个表 shee t然后添加字段 名users?int columns = dataGridView1.Columns.Count;/列数int rows = dataGridView1.Rows.Count;/行数for (int j = 0; j columns; j+)sheet.Cells0, j.V

6、alue = dataGridView1.Columnsj.HeaderText; 添加第一行数据, 应该是添加字段名吧。for (int i = 1; i rows; i+)for (int j = 0; j columns; j+)sheet.Cellsi, j.Value = dataGridView1j, i - 1.Value.ToString().Trim();/ 从第二行开始添加数据excelFile.SaveXls(./Users.xls); /保存为excel 表MessageBox.Show(生成成功);private void button2_Click(object s

7、ender, EventArgs e)SaveFileDialog saveFileDialog = new SaveFileDialog();saveFileDialog.Filter = Execl files (*.xls)|*.xls”;saveFileDialog.FilterIndex = 0;saveFileDialog.RestoreDirectory = true;saveFileDialog.CreatePrompt = true;/saveFileDialog.Title =导出 Exce l文件到;DateTime now = DateTime.Now;saveFile

8、Dialog.FileName = now.Year.ToString().PadLeft(2) + now.Month.ToString().PadLeft(2, 0) + now.Day.ToString().PadLeft(2, 0) + - + now.Hour.ToString().PadLeft(2, 0) + now.Minute.ToString().PadLeft(2, 0) + now.Second.ToString().PadLeft(2, 0);saveFileDialog.ShowDialog();Stream myStream;myStream = saveFile

9、Dialog.OpenFile();StreamWriter sw = new StreamWriter(myStream, System.Text. Encoding.GetEncoding(gb2312”); string str =”; try /写标题 for (int i = 0; i 0) str += t”;str += dataGridView1.Columnsi.HeaderText;sw.WriteLine(str);/写内容for (int j = 0; j dataGridViewl.Rows.Count; j+)string tempStr =”;for (int k = 0; k 0)tempStr += t”;tempStr += dataGridView1.Rowsj.Cellsk.Value.ToString();sw.WriteLine(tempStr);sw.Close();myStream.Close();MessageBox

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论