在TextBox中显示Excel数据和文本文件数据 [英] Displaying Excel Data and text file data in TextBox

查看:181
本文介绍了在TextBox中显示Excel数据和文本文件数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 这是克里希纳
我必须开发一个在文本框中显示excel或文本文件数据的应用程序
请参阅以下屏幕(如何添加图片)->我不知道,所以我在下面描述

形式:
手机号码文本框

上传按钮

当我单击上载按钮时,它必须仅显示excel和txt文件
当我单击excel或文本文件(如果其中包含手机号码)时,这些将显示在带有逗号的文本框中
我的代码如下:

 私有 无效 lnklUpload_LinkClicked(对象发​​件人,LinkLabelLinkClickedEventArgs e)
       {
           OpenFileDialog browserFile =  OpenFileDialog();
           BrowseFile.Filter = " ;
           BrowseFile.Title = " ;
           如果(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数据和文本文件数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆