winform Execl导入问题

csdn的黄公子 2012-11-28 03:25:48
最近做一个 winform 项目,实现Execl导入功能。在网上看了些例子,有疑问,请高手指教!

如何把Execl的 数据插入 表中 ?? 请高手们提供例子。。


是insert into 表 values(),因为表名称动态参数,如何Execl数据导入表中。

...全文
217 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
csdn的黄公子 2012-11-29
  • 打赏
  • 举报
回复
针对,一个Execl中 sheet1 ,sheet2,sheet3,sheet4.怎么insert 到sheet1 。。。表中去呢。。
vssvss 2012-11-29
  • 打赏
  • 举报
回复
我来给你个例子 可能有点长哦 耐心看: using Excel = Microsoft.Office.Interop.Excel; private void readExcel(string excelpath) { object TypeMissing = System.Reflection.Missing.Value; try { Excel.Application myExcel = null; //Excel.Workbook myBook = null; Excel.Worksheet mySheet = null; String xmlName = String.Empty; myExcel = new Excel.Application(); //Open the excel file myExcel.Application.Workbooks.Open(excelpath, TypeMissing, TypeMissing, TypeMissing, TypeMissing, TypeMissing, TypeMissing, TypeMissing, TypeMissing, TypeMissing, TypeMissing, TypeMissing, TypeMissing, TypeMissing, TypeMissing); int sheetNo=myExcel.Sheets.Count; int index = 1; String tempXml = String.Empty; String tagTemp = String.Empty; String content = String.Empty; //Read the sheet [1] of this excel. for (int i = 1; i <= sheetNo; i++) { //myBook = myExcel.Workbooks[index]; mySheet = (Excel.Worksheet)myExcel.Worksheets[i]; xmlName = mySheet.Name + ".xml"; //Make sure the xml is the lastest one. if (File.Exists(xmlName)) { File.Delete(xmlName); } #region write data to the xml. //Open the xml file ready to write content into it. FileStream objFileStream = new FileStream(xmlName, FileMode.Append, FileAccess.Write); StreamWriter objStreamWriter = new StreamWriter(objFileStream); //The title of the xml file. String xmlTitle = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"; objStreamWriter.WriteLine(xmlTitle); objStreamWriter.WriteLine("<" + mySheet.Name + "Locales" + ">"); //The total row, colunm number of the used row of this sheet. int rowTotal = mySheet.UsedRange.Rows.Count; int colunmTotal = mySheet.UsedRange.Columns.Count; //The read row, colunm number when this read. int row = 0; int colunm = 0; index = 1; //Those intermediate variables when this read. for (colunm = 2; colunm <= colunmTotal; colunm++) { String rootNode = "Language"; objStreamWriter.WriteLine("<" + rootNode + ">"); for (row = 1; row <= rowTotal; row++) { //The content is no empty tagTemp = ((Excel.Range)mySheet.UsedRange.Cells[row, 1]).Text.ToString().Trim(); if (tagName.Length < 1) { tagName = "UnSure"; } content = ((Excel.Range)mySheet.UsedRange.Cells[row, colunm]).Text.ToString().TrimEnd(); if (content.Length < 1) { content = ((Excel.Range)mySheet.UsedRange.Cells[row, 2]).Text.ToString().TrimEnd(); } //The content of this tag,such as: //<tagTemp>content</tagTemp> tempXml = "<" + tagTemp + ">" + content.Replace("&", "&") + "<" + tagTemp.Insert(0, "/") + ">"; objStreamWriter.WriteLine(tempXml); this.mResult.AppendText((index++) + " :" + tempXml + "\r\n"); } objStreamWriter.WriteLine("</" + rootNode + ">\r\n"); } objStreamWriter.WriteLine("</" + mySheet.Name + "Locales" + ">"); objStreamWriter.Close(); #endregion //Open the result xml file by NotePad. System.Diagnostics.Process.Start("notepad.exe", xmlName); } //Close those object after used. mySheet = null; myExcel.Quit(); GC.Collect(); } catch (Exception e) { this.mResult.AppendText(e.Message.ToString() + "\r\n"); } }
Change_L 2012-11-28
  • 打赏
  • 举报
回复
string name=new OleDbConnection(excel路径).GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new Object[] { null, null, null, "TABLE" });获取excel工作表名称 DataTable dt=new DataTable(); string sql = "select * from [" + name + "]"; OleDbDataAdapter adapter = new OleDbDataAdapter(sql, conn); adapter.Fill(dt);
F15Eagle 2012-11-28
  • 打赏
  • 举报
回复
以前弄过一个,先有一个按钮,点击去选excel文件,然后把excel文件的内容读取到DataGridView,然后把不匹配的数据标红(例如长度超长的那种,或者不是数值的),然后用户可以修改,改完之后再点击另外一个按钮,把数据从DataGridView导入到sqlserver。基本过程是这样的,代码没了。。。
csdn的黄公子 2012-11-28
  • 打赏
  • 举报
回复
那是我的代码。我怎么插入到 表中去。。。这个是问题
cjh200102 2012-11-28
  • 打赏
  • 举报
回复
楼上可以。。
csdn的黄公子 2012-11-28
  • 打赏
  • 举报
回复
stringstrConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filePath + ";" + "Extended Properties=Excel 8.0;"; OleDbConnection con = new OleDbConnection(strConn); con.Open(); string[] names = GetExcelSheetNames(filePath); if (names.Length> 0) { foreach (string name in names) { OleDbCommandcmd = con.CreateCommand(); cmd.CommandText = string.Format(" select * from [{0}$]", name) ;//[sheetName$]要如此格式 OleDbDataReaderodr = cmd.ExecuteReader(); while (odr.Read()) { //////如何 insert 到表中去。 } odr.Close(); } } return true; } catch (Exception) { return false; }

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧