在TextBox中显示Excel数据和文本文件数据

编程入门 行业动态 更新时间:2024-10-28 17:14:22
本文介绍了在TextBox中显示Excel数据和文本文件数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 嗨 这是克里希纳 我必须开发一个在文本框中显示excel或文本文件数据的应用程序 请参阅以下屏幕(如何添加图片)->我不知道,所以我在下面描述 形式: 手机号码文本框 上传按钮 当我单击上载按钮时,它必须仅显示excel和txt文件 当我单击excel或文本文件(如果其中包含手机号码)时,这些将显示在带有逗号的文本框中 我的代码如下:

私有 无效 lnklUpload_LinkClicked(对象发​​件人,LinkLabelLinkClickedEventArgs e) { OpenFileDialog browserFile = 新 OpenFileDialog(); BrowseFile.Filter = " Excel文件(* .xlsx; *.xls; *.txt)| * .xlsx; * .xls; *.txt"; BrowseFile.Title = " 浏览到Excel 2007文件" ; 如果(browseFile.ShowDialog()== DialogResult.Cancel) 返回; 尝试 { txtMobileNumber.Text = BrowseFile.FileName; 字符串 conExcel = txtMobileNumber.Text; // SetExcel(conExcel); } 捕获(异常) { MessageBox.Show(" 打开文件时出错" ," 文件错误", MessageBoxButtons.OK,MessageBoxIcon.感叹号); } }

我实现了: 1.只显示txt,excel文件对我来说很好 我必须实现: 1.必须同时显示手机号码 例如,如果我在excel表格中有10个手机号码,则在该excel表格上单击时,它必须在文本框中显示该手机号码,如下所示 8765432567,9876543567,...........

解决方案

要打开文件,您需要一个OpenFileDialog. 将OpenFileDialog筛选器属性设置为".xls",.txt"以仅显示所需的文件. 在您的上传按钮上,单击事件使用: openfiledialog.showdialog() 在OpenFileDialog FileOk事件上,您将代码用于读取文件内容并将其显示在文本框中.

这里是一些示例代码,是VB,但可以轻松转换为C#: 您需要添加对Microsoft.Office.Interop.Excel的引用. Dim oXL作为Microsoft.Office.Interop.Excel.Application 作为Microsoft.Office.Interop.Excel.Workbook的Dim oBook 作为Microsoft.Office.Interop.Excel.Worksheet的Dim oSheet 昏暗vValue作为对象 oXL =新的Microsoft.Office.Interop.Excel.Application oBook = oXL.Workbooks.Open(OpenFileDialog1.FileName) oSheet = oBook.Worksheets("Sheet1") ``这里您具有单元格值,只需构建一个循环即可获取所需的数据 ''这将循环遍历第一列中的所有行 对于i As Integer = 0到oSheet.Rows.Count vValue = vValue +;" + oSheet.Cells(1,1).Value 下一个 myTextBox.Text = vValue

hi this is Krishna I have to develop one application which displays excel or text file data in text box see the below screens(how to add pictures)-->i don''t know so i describe below Form: mobile numbers Text box upload button when i click on upload button it has to show only excel and txt files only and when i click on excel or text file if it contains the mobile numbers then those are display in text box with comas my code is like below:

private void lnklUpload_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { OpenFileDialog browseFile = new OpenFileDialog(); browseFile.Filter = "Excel Files (*.xlsx;*.xls;*.txt)|*.xlsx;*.xls;*.txt"; browseFile.Title = "Browse To Excel 2007 File"; if (browseFile.ShowDialog() == DialogResult.Cancel) return; try { txtMobileNumber.Text = browseFile.FileName; string conExcel = txtMobileNumber.Text; //SetExcel(conExcel); } catch (Exception) { MessageBox.Show("Error opening file", "File Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }

I achieved: 1.it shows txt,excel files only that is fine for me I have to achieve: 1.it has to display mobile numbers side by side for example if i have 10 mobile numbers in excel sheet when click on that excel sheet it has to show the mobile numbers in textbox like below 8765432567,9876543567,...........

解决方案

For opening the file you need a OpenFileDialog. Set the OpenFileDialog filter property to ".xls",".txt" to show only the files you want. On your upload button click event use: openfiledialog.showdialog() On the OpenFileDialog FileOk Event you put your code for read the file contents and displaying them on textbox.

Here''s some sample code, is VB but is easily convertible to C#: You need to add a reference to Microsoft.Office.Interop.Excel. Dim oXL As Microsoft.Office.Interop.Excel.Application Dim oBook As Microsoft.Office.Interop.Excel.Workbook Dim oSheet As Microsoft.Office.Interop.Excel.Worksheet Dim vValue As Object oXL = New Microsoft.Office.Interop.Excel.Application oBook = oXL.Workbooks.Open(OpenFileDialog1.FileName) oSheet = oBook.Worksheets("Sheet1") ''Here you have cell value, just build a loop to get the data you want ''This will loop through all rows in the first column For i As Integer = 0 To oSheet.Rows.Count vValue = vValue + ";" + oSheet.Cells(1, 1).Value Next myTextBox.Text = vValue

更多推荐

在TextBox中显示Excel数据和文本文件数据

本文发布于:2023-07-09 00:17:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1082108.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   文本文件   TextBox   Excel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!