View Code
 1         /// <summary>
 2         /// 使用OLEDB对excel文件进行读取,将Excel文件作为数据源
 3         /// </summary>
 4         /// <param name="p"></param>
 5         /// <returns></returns>
 6         private DataSet ExcelToDataSet(string fileName)
 7         {
 8             //定义连接字符串
 9             string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\";" + "data source=" + fileName;
10 
11             //定义连接对象
12             OleDbConnection conn = new OleDbConnection(strConn);
13             try
14             {
15                 //打开连接
16                 conn.Open();
17                 string strExcel = "";
18                 //数据适配器
19                 OleDbDataAdapter myCommand = null;
20                 //数据集
21                 DataSet ds = null;
22 
23                 //通过索引来获取工作表的集合
24                 System.Data.DataTable schemaTable = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
25                 //获取工作表名
26                 string tableName = schemaTable.Rows[0][2].ToString().Trim();
27                 //查询语句
28                 strExcel = "select * from [" + tableName + "]";
29                 myCommand = new OleDbDataAdapter(strExcel, strConn);
30                 ds = new DataSet();
31                 //填充数据
32                 myCommand.Fill(ds, "table1");
33                 conn.Close();
34                 return ds;
35             }
36             catch
37             {
38                 MessageBox.Show("你选择的文件不是标准的Excel文件");
39                 return null;
40             }
41         }
View Code
 1         /// <summary>
 2         /// 将datagridview的数据以文件流的形式写入Excel文件
 3         /// </summary>
 4         /// <param name="dgv"></param>
 5         public void DataGridViewToExcel(DataGridView dgv)
 6         {
 7             SaveFileDialog dlg = new SaveFileDialog();
 8             dlg.Filter = "Execl files (*.xls)|*.xls";
 9             dlg.CheckFileExists = false;
10             dlg.CheckPathExists = false;
11             dlg.FilterIndex = 0;
12             dlg.RestoreDirectory = true;
13             dlg.CreatePrompt = false;
14             dlg.Title = "保存为Excel文件";
15 
16             if (dlg.ShowDialog() == DialogResult.OK)
17             {
18                 Stream myStream;
19                 myStream = dlg.OpenFile();
20                 StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
21                 string columnTitle = "";
22                 try
23                 {
24                     //写入列标题  
25                     for (int i = 0; i < dgv.ColumnCount; i++)
26                     {
27                         if (i > 0)
28                         {
29                             columnTitle += "\t";
30                         }
31                         columnTitle += dgv.Columns[i].HeaderText;
32                     }
33                     sw.WriteLine(columnTitle);
34 
35                     //写入列内容  
36                     for (int j = 0; j < dgv.Rows.Count; j++)
37                     {
38                         string columnValue = "";
39                         for (int k = 0; k < dgv.Columns.Count; k++)
40                         {
41                             if (k > 0)
42                             {
43                                 columnValue += "\t";
44                             }
45                             if (dgv.Rows[j].Cells[k].Value == null)
46                                 columnValue += "";
47                             else
48                                 columnValue += dgv.Rows[j].Cells[k].Value.ToString().Trim();
49                         }
50                         sw.WriteLine(columnValue);
51                     }
52                     sw.Close();
53                     myStream.Close();
54                     MessageBox.Show("数据导出成功!", "提示", MessageBoxButtons.OK);
55                 }
56                 catch (Exception e)
57                 {
58                     MessageBox.Show(e.ToString());
59                 }
60                 finally
61                 {
62                     sw.Close();
63                     myStream.Close();
64                 }
65             }
66         }
posted on 2012-05-11 14:26  捣乃忒  阅读(144)  评论(0编辑  收藏  举报