zoukankan      html  css  js  c++  java
  • Excel 操作(未完待续)

    Excel导入 数据库

    View Code
     1 private void GetExcelData07(string str)
     2         {
     3             try
     4             {
     5                 strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + str + "';Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
     6                 OleDbConnection myConn = new OleDbConnection(strCon);
     7                 myConn.Open();
     8                 DataTable dtTables = myConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
     9                 myConn.Close();
    10                 DataSet myDataSet = new DataSet();
    11                 if (dtTables.Rows.Count > 0)
    12                 {
    13                     lb_Tag.Items.Clear();
    14                     foreach (DataRow dr in dtTables.Rows)
    15                     {
    16                         string sSName = dr["TABLE_NAME"].ToString();
    17                         if (!sSName.Contains("Print_Titles") && !sSName.Contains("Print_Area")) lb_Tag.Items.Add(sSName);
    18                     }
    19                 }
    20             }
    21             catch (Exception ex)
    22             {
    23                 System.Windows.Forms.MessageBox.Show(ex.Message, "您选择的数据导入的EXCEL不是正确的EXCEL!");
    24             }
    25         }
    26 
    27         private void GetExcelData03(string str)
    28         {
    29             try
    30             {
    31                 strCon = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source ='" + str + "';Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
    32                 OleDbConnection myConn = new OleDbConnection(strCon);
    33                 myConn.Open();
    34                 DataTable dtTables = myConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
    35                 myConn.Close();
    36                 if (dtTables.Rows.Count > 0)
    37                 {
    38                     lb_Tag.Items.Clear();
    39                     foreach (DataRow dr in dtTables.Rows)
    40                     {
    41                         string sSName = dr["TABLE_NAME"].ToString();
    42                         if (!sSName.Contains("Print_Titles") && !sSName.Contains("Print_Area")) lb_Tag.Items.Add(sSName);
    43                     }
    44                 }
    45             }
    46             catch (Exception ex)
    47             {
    48                 System.Windows.Forms.MessageBox.Show(ex.Message, "您选择的数据导入的EXCEL不是正确的EXCEL!");
    49             }
    50         }
    51 
    52         private DataSet SelectTable(string sSName)
    53         {
    54             DataSet myDataSet = new DataSet();
    55             OleDbConnection myConn = new OleDbConnection(strCon);
    56             string strCom = String.Format("select * from [{0}]", sSName);
    57             myConn.Open();
    58             OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);
    59             myCommand.Fill(myDataSet);
    60             myConn.Close();
    61             return myDataSet;
    62         }
    63 
    64 
    65         private void btn_Select_Click(object sender, EventArgs e)
    66         {
    67             OpenFileDialog MyFilePath = new OpenFileDialog();
    68             MyFilePath.Title = "打开文件";
    69             MyFilePath.Filter = "Excel2003文件|*.xls|Excel2007文件|*.xlsx";
    70             if (MyFilePath.ShowDialog() == DialogResult.OK)
    71             {
    72 
    73                 str_filepath = MyFilePath.FileName.ToString();
    74                 tb_FileName.Text = MyFilePath.FileName.ToString();
    75             }
    76             else
    77             {
    78                 return;
    79             }
    80             DataTable dt = new DataTable();
    81             if (str_filepath.Contains(".xlsx"))
    82             {
    83                 GetExcelData07(str_filepath);
    84             }
    85             else
    86             {
    87                 GetExcelData03(str_filepath);
    88             }
    89             ds = SelectTable(lb_Tag.Items[0].ToString());
    90             if (ds != null && ds.Tables.Count > 0)
    91             {
    92                 gridControl.DataSource = CreateDataTable(ds);
    93             }
    94             else
    95             {
    96                 tb_FileName.Text = string.Empty;
    97             }
    98         }

    OleDbConnection访问Excel 定义过多字段异常

    View Code
    1 在查询中,Excel的spreedsheet的列数有256的限制。
    2 因此在查询时odCommand.CommandText = "SELECT * FROM ["+sheet+"]"; 改成 "SELECT * FROM ["+sheet+"A:IU]"
    3 行数应该也有限制。但是行数限制要比列数宽松。这里就没有对行做限制。
    4 这样暂时就解决问题了。
  • 相关阅读:
    跟我一起学习ASP.NET 4.5 MVC4.0(二)(转)
    跟我一起学习ASP.NET 4.5 MVC4.0(一)(转)
    MVC3 Razor视图引擎的基础语法
    了解ASP.NET MVC几种ActionResult的本质:HttpStatusCodeResult & RedirectResult/RedirectToRouteResult
    通过一个模拟程序让你明白ASP.NET MVC是如何运行的
    ASP.NET中后台注册js脚本攻略(转)
    Python: 什么是*args和**kwargs
    配置mysql 及 设置密码
    java静态方法Static
    并发程序设计知识总结
  • 原文地址:https://www.cnblogs.com/wanghafan/p/2487711.html
Copyright © 2011-2022 走看看