日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

C# 实现Oracle中的数据与Excel之间的转换

發布時間:2023/11/27 生活经验 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# 实现Oracle中的数据与Excel之间的转换 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近項目要求實現數據庫之間數據在各個數據庫之間導入導出,在此做個筆記

1. 將Oracle中的表導入到Excel中,反之亦然

  ?private static readonly string connectionString = ConfigurationManager.ConnectionStrings["OracleConnection"].ConnectionString;
1
public void print(DataGridView dataGridView1) 2 { 3 //導出到execl 4 try 5 { 6 SaveFileDialog saveFileDialog = new SaveFileDialog(); 7 saveFileDialog.Filter = "導出Excel2003~2007 (*.xls)|*.xls|導出Excel2010~2013 (*.xlsx)|*.xlsx"; 8 saveFileDialog.FilterIndex = 0; 9 saveFileDialog.RestoreDirectory = true; 10 saveFileDialog.CreatePrompt = true; 11 saveFileDialog.Title = "導出文件保存路徑"; 12 saveFileDialog.ShowDialog(); 13 string strName = saveFileDialog.FileName; 14 if (strName.Length != 0) 15 { 16 //沒有數據的話就不往下執行 17 if (dataGridView1.Rows.Count == 0) 18 return; 19 20 // toolStripProgressBar1.Visible = true; 21 System.Reflection.Missing miss = System.Reflection.Missing.Value; 22 //實例化一個Excel.Application對象 23 Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); 24 excel.Application.Workbooks.Add(true); 25 excel.Visible = false;//若是true,則在導出的時候會顯示EXcel界面。 26 if (excel == null) 27 { 28 MessageBox.Show("EXCEL無法啟動!", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); 29 return; 30 } 31 Microsoft.Office.Interop.Excel.Workbooks books = (Microsoft.Office.Interop.Excel.Workbooks)excel.Workbooks; 32 Microsoft.Office.Interop.Excel.Workbook book = (Microsoft.Office.Interop.Excel.Workbook)(books.Add(miss)); 33 Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.ActiveSheet; 34 sheet.Name = "test"; 35 int m = 0, n = 0; 36 //生成Excel中列頭名稱 37 for (int i = 0; i < dataGridView1.Columns.Count; i++) 38 { 39 excel.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText;//輸出DataGridView列頭名 40 } 41 //把DataGridView當前頁的數據保存在Excel中 42 for (int i = 0; i < dataGridView1.Rows.Count - 1; i++) 43 { 44 for (int j = 0; j < dataGridView1.Columns.Count; j++) 45 { 46 if (dataGridView1[j, i].ValueType == typeof(string)) 47 { 48 excel.Cells[i + 2, j + 1] = "'" + dataGridView1[j, i].Value.ToString(); 49 } 50 else 51 { 52 excel.Cells[i + 2, j + 1] = dataGridView1[j, i].Value.ToString(); 53 } 54 } 55 } 56 sheet.SaveAs(strName, miss, miss, miss, miss, miss, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss); 57 book.Close(false, miss, miss); 58 books.Close(); 59 excel.Quit(); 60 System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet); 61 System.Runtime.InteropServices.Marshal.ReleaseComObject(book); 62 System.Runtime.InteropServices.Marshal.ReleaseComObject(books); 63 System.Runtime.InteropServices.Marshal.ReleaseComObject(excel); 64 GC.Collect(); 65 MessageBox.Show("數據已經成功導出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 66 // toolStripProgressBar1.Value = 0; 67 System.Diagnostics.Process.Start(strName); 68 } 69 } 70 catch (Exception ex) 71 { 72 MessageBox.Show(ex.Message, "錯誤提示"); 73 } 74 }
 1    public void printAll(System.Data.DataTable dt)
 2         {
 3             //導出到execl
 4             try
 5             {
 6                 SaveFileDialog saveFileDialog = new SaveFileDialog();
 7                 saveFileDialog.Filter = "導出Excel (*.xls)|*.xls";
 8                 saveFileDialog.FilterIndex = 0;
 9                 saveFileDialog.RestoreDirectory = true;
10                 saveFileDialog.CreatePrompt = true;
11                 saveFileDialog.Title = "導出文件保存路徑";
12                 saveFileDialog.ShowDialog();
13                 string strName = saveFileDialog.FileName;
14                 if (strName.Length != 0)
15                 {
16                     //沒有數據的話就不往下執行
17                     if (dt.Rows.Count == 0)
18                         return;
19 
20                     // toolStripProgressBar1.Visible = true;
21                     System.Reflection.Missing miss = System.Reflection.Missing.Value;
22                     //實例化一個Excel.Application對象
23                     Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
24                     excel.Application.Workbooks.Add(true);
25                     excel.Visible = false;//若是true,則在導出的時候會顯示EXcel界面。
26                     if (excel == null)
27                     {
28                         MessageBox.Show("EXCEL無法啟動!", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
29                         return;
30                     }
31                     Microsoft.Office.Interop.Excel.Workbooks books = (Microsoft.Office.Interop.Excel.Workbooks)excel.Workbooks;
32                     Microsoft.Office.Interop.Excel.Workbook book = (Microsoft.Office.Interop.Excel.Workbook)(books.Add(miss));
33                     Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.ActiveSheet;
34                     sheet.Name = "test";
38                     //生成Excel中列頭名稱
39                     for (int i = 0; i < dt.Columns.Count; i++)
40                     {
41                         excel.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText;//輸出DataGridView列頭名
42                     }
43 
44                     //把DataGridView當前頁的數據保存在Excel中
45                     if (dt.Rows.Count > 0)
46                     {
47                         for (int i = 0; i < dt.Rows.Count; i++)//控制Excel中行,上下的距離,就是可以到Excel最下的行數,比數據長了報錯,比數據短了會顯示不完
48                         {
49                             for (int j = 0; j < dt.Columns.Count; j++)//控制Excel中列,左右的距離,就是可以到Excel最右的列數,比數據長了報錯,比數據短了會顯示不完
50                             {
51                                 string str = dt.Rows[i][j].ToString();
52                                 excel.Cells[i + 2, j + 1] = "'" + str;//i控制行,從Excel中第2行開始輸出第一行數據,j控制列,從Excel中第1列輸出第1列數據,"'" +是以string形式保存,所以遇到數字不會轉成16進制
53                             }
54                         }
55                     }
56                     sheet.SaveAs(strName, miss, miss, miss, miss, miss, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss);
57                     book.Close(false, miss, miss);
58                     books.Close();
59                     excel.Quit();
60                     System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
61                     System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
62                     System.Runtime.InteropServices.Marshal.ReleaseComObject(books);
63                     System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
64 
65                     GC.Collect();
66                     MessageBox.Show("數據已經成功導出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
67                     // toolStripProgressBar1.Value = 0;
68                     System.Diagnostics.Process.Start(strName);
69                 }
70             }
71             catch (Exception ex)
72             {
73                 MessageBox.Show(ex.Message, "錯誤提示");
74             }
75         }

這兩個方法都可以將Oracle導出到Excel中。轉自http://www.cnblogs.com/cpcpc/archive/2012/11/13/2767934.html

 1 class Excel2Oracle
 2     {
 3         private static readonly string connectionString = ConfigurationManager.ConnectionStrings["OracleConnection"].ConnectionString;
 4         public void InsertData2Oracle(DataSet ds)
 5         {
 6             using (OleDbConnection oraCon = new OleDbConnection(connectionString))
 7             {
 8                 oraCon.Open();
 9                 using (OleDbCommand cmd = new OleDbCommand())
10                 {
11                     cmd.Connection = oraCon;
12                     cmd.CommandType = CommandType.Text;
13 
14                     for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
15                     {
16                         List<string> list_ = new List<string>();
17                         for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
18                         {
19                             list_.Add(ds.Tables[0].Rows[i][j].ToString());
20                         }
21                         string temp = "";
22                         string t = ",";
23 
24                         for (int k = 0; k < list_.Count; k++)
25                         {
26                             if (k == list_.Count - 1) t = "";
27                             temp = temp + "'" + list_[k] + "'" + t + "";
28                             cmd.CommandText = string.Format("Insert into TBUSER values(" + temp + ")");
29                         }
30 
31                         cmd.ExecuteNonQuery();
32                     }
33 
34                 }
35                 oraCon.Close();
36 
37             }
38         }
    }  

代碼比廢話更要迷人,我就不多說什么了

轉載于:https://www.cnblogs.com/jnzdn/p/5473595.html

總結

以上是生活随笔為你收集整理的C# 实现Oracle中的数据与Excel之间的转换的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。