日期:2014-05-18 浏览次数:21001 次
string filename = ""; if (DialogResult.OK == saveFileDlg.ShowDialog()) { filename = saveFileDlg.FileName; //execl 导出 String source = null; OdbcConnection conn = null; try { source = "Driver={Microsoft Excel Driver (*.xls)};FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\""+filename +"\";DBQ=" + filename ; conn = new OdbcConnection(source); conn.Open(); } catch { try { source = "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\""+filename +"\";DBQ=" + filename ; conn = new OdbcConnection(source); conn.Open(); } catch { MessageBox.Show("请确认此文件没有被其它程序打开!"); return; } } if (dataSet1.Tables.Count <= 0) return; try { string s = ""; string f = ""; string[] numlx = new string[] { "System.Int32", "System.Int16", "System.Int64", "System.Decimal", "System.Single", "System.Double" }; for (int i = 0; i < dataSet1.Tables[0].Columns.Count; i++) { if (numlx.Contains(dataSet1.Tables[0].Columns[i].DataType.ToString())) { s = s + ",[" + dataSet1.Tables[0].Columns[i].ColumnName + "] NUMBER"; } else { s = s +",[" +dataSet1.Tables[0].Columns[i].ColumnName + "] TEXT"; } f = f + ",[" + dataSet1.Tables[0].Columns[i].ColumnName+"]"; } s = "CREATE TABLE " + dataSet1.Tables[0].TableName + "(" + s.Substring(1) + ")"; f = "insert into " + dataSet1.Tables[0].TableName + "(" + f.Substring(1) + ") values("; OdbcCommand cmd1 = new OdbcCommand(s, conn); cmd1.ExecuteNonQuery(); foreach (DataRow dr in dataSet1.Tables[0].Rows) { string sz = ""; for (int i = 0; i < dataSet1.Tables[0].Columns.Count; i++) { if (dr[i] != DBNull.Value) { sz = sz + ",'" + dr[i].ToString() + "'"; } else { sz = sz + ",null"; } }