winform导入excel文件|C# winform 中如何导入Excel

winform导入excel文件|C# winform 中如何导入Excel的第1张示图

⑴ winform 如何导入导出EXCEL

首先在你的项目里添加引用Microsoft.Office.Interop.Excel.dll然后//创建对象Microsoft.Office.Interop.Excel.Applicationecl=newMicrosoft.Office.Interop.Excel.Application();ecl.Workbooks.Add(true);//允许工作区域放东西//举例子ecl.Cells[1,1]="姓名";ecl.Cells[1,2]="学号";ecl.Cells[1,3]="性别";ecl.Cells[1,4]="年龄";ecl.Cells[1,5]="座位";ecl.Cells[1,6]="住址";//////////////////////for(inti=2;i<ds.Tables["stuInfo"].Rows.Count+2;i++){ecl.Cells[i,1]=ds.Tables["stuInfo"].Rows[i-2][0].ToString();ecl.Cells[i,2]=ds.Tables["stuInfo"].Rows[i-2][1].ToString();ecl.Cells[i,3]=ds.Tables["stuInfo"].Rows[i-2][2].ToString();ecl.Cells[i,4]=ds.Tables["stuInfo"].Rows[i-2][3].ToString();ecl.Cells[i,5]=ds.Tables["stuInfo"].Rows[i-2][4].ToString();ecl.Cells[i,6]=ds.Tables["stuInfo"].Rows[i-2][5].ToString();}ecl.Visible=true;//显示EXCEL

⑵ 如何将Winfrom中的数据导入固定的excel模版中C#

先读excel模版,根据excel模版中的标识获取winfrom中的数据填入对应的单元格中,然后excel另存为。这个其实主要就是对excel的操作了,具体对Excel的操作教程网上有很多,随便搜搜都有的。

⑶ 求用winform导入导出excel的方法!!!另外我在调试时出错!!!

在这里补充一下,如果你的电脑安装了microso offices vs中的操作都好了还是出错的话,你看看你的offices办公软件安装在c盘,还是其他盘下。我当时遇到的问题是这样,我的是安装在d:盘了,结果换了c:盘就好了。

⑷ winform中如何将不规则的excel数据导入界面中

读取Excel

存入数据集dataset

保存数据库

读取数据库Gridview提取

⑸ C#Winform怎么将excel一列数据导入程序中

/// <summary> /// 导入Excel到DataSet /// </summary> /// <param name="filePath"></param> /// <returns></returns> private DataSet InExecl(string filePath) { DataSet ds = new DataSet(); string connStr = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " + filePath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\""; OleDbConnection myConn = new OleDbConnection(connStr); string strSQL = " SELECT * FROM [Sheet1$]"; myConn.Open(); OleDbDataAdapter myCommand = new OleDbDataAdapter(strSQL, myConn); DataSet myDataSet = new DataSet(); myCommand.Fill(myDataSet, "[Sheet1$]"); myConn.Close(); return myDataSet; } /// <summary> /// 导入 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnIn_Click(object sender, EventArgs e) { DataSet ds = InExecl(this.File1.PostedFile.FileName); this.GridView2.DataSource = ds; this.GridView2.DataBind(); }

⑹ C# winform 中如何导入Excel

你是要从excel中导入数据到winform吗?如果是这样,可以这样:引用office11.0组件后, Microsoft.Office.Interop.Excel.Application application; //这是一个客户端 Microsoft.Office.Interop.Excel.Workbooks workbooks; //所有工作薄 Microsoft.Office.Interop.Excel.Worksheet worksheet;//工作表 Microsoft.Office.Interop.Excel.Workbook workbook; //所用到的工作表 void IsRunEX(){ OpenFileDialog openfilediaglog = new OpenFileDialog(); openfilediaglog.Filter = "xls文件|*.xls"; if (openfilediaglog.ShowDialog() == DialogResult.OK) { FieldName = openfilediaglog.FileName; application = new Microsoft.Office.Interop.Excel.Application(); workbooks = application.Workbooks; workbook = returnworkbook(FieldName, workbooks); worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1]; //选择第一个表 Range range = worksheet.Cells[1, 8] as Range; //这是选择第一行第八列的内容 Range rangee = worksheet.Cells[1, 9] as Range; //这是第一行到九列的内容 string str1=range.Value2.ToString(); string str2=ragee.Value2.ToString(); ………………. //你所要做的操作 ……………… workbook.Close(Type.Missing, FieldName, Type.Missing); workbooks.Close(); //退出关闭资源 application.Quit(); }} private Workbook returnworkbook(string filename,Workbooks works) //这里是打一开一个工作表 { Microsoft.Office.Interop.Excel.Workbook wk=works.Open( filename, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); return wk; }

⑺ c# winform 中导入Excel 的问题 !

导入命名空间:using System.Data.OleDb; OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + FilePath + ";Extended Properties=Excel 8.0;"); conn.Open(); string strExcel = ""; OleDbDataAdapter myCommand = null; strExcel = string.Format("select * from [{0}$]", comboBox1.Text); myCommand = new OleDbDataAdapter(strExcel, "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + FilePath + ";Extended Properties=Excel 8.0;"); myCommand.Fill(ds, comboBox1.Text);Filepath为选择的excel文件本地路径

⑻ 如何用winform向excel中插入数据,就好像把excel当作数据库是一样的,我只会BS的 CS不会写 求教

这里我用了Execl.dll的方式导出,不过这种方式用很大的弊端…貌似要根据office版本来确定引用的类…ps:我也不太明白第一步引用命名空间关于这个命名空间晚上的资料多大直接都写的是using Excel;private void doExport(DataTable dt, string strSheetName) { Excel.Application excel = new Excel.Application(); //Execl的操作类 //读取保存目标的对象 Excel.Workbook bookDest = (Excel.WorkbookClass)excel.Workbooks.Add(Missing.Value); Excel.Worksheet sheetDest = bookDest.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value) as Excel.Worksheet;//给工作薄添加一个Sheet sheetDest.Name = strSheetName;//自己定义名字O(∩_∩)O哈哈~ int rowIndex = 1; int colIndex = 0; excel.Application.Workbooks.Add(true);//这句不写不知道会不会报错 foreach (DataColumn col in dt.Columns) { colIndex++; sheetDest.Cells[1, colIndex] = col.ColumnName;//Execl中的第一列,把DataTable的列名先导进去 } //导入数据行 foreach (DataRow row in dt.Rows) { rowIndex++; colIndex = 0; foreach (DataColumn col in dt.Columns) { colIndex++; sheetDest.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString(); } } bookDest.Saved = true; bookDest.SaveCopyAs("要保存的文件路径+文件名.xls");//方式一 excel.Quit(); excel = null; GC.Collect();//垃圾回收 }

未经允许不得转载:山九号 » winform导入excel文件|C# winform 中如何导入Excel

赞 (0)