如何在文本框中显示Excel单元格值 [英] how to display an excel cell value into a text box
本文介绍了如何在文本框中显示Excel单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在文本框中显示Excel单元格值
解决方案
假设您有一个Excel文件 SampleFile.xls ,其中包含1标题年龄以下的列有以下某些值,您必须将该列的最后一个值读入文本框,然后您可以按照以下步骤操作:
1.首先在aspx页面上添加标签和文本框。这是将显示Excel文件值的文本框。
< asp:label id = Label1 runat = 服务器 text = 年龄 > < / asp:label >
< asp:textbox id = TextBox1 runat = server > < span class =code-keyword>< / asp:textbox >
2.在代码隐藏文件中编写以下代码以获取值一个textb来自excel文件的牛。
// 创建我们可以在成员函数中使用的Application对象。
Microsoft.Office.Interop.Excel.Application _excelApp = new Microsoft.Office.Interop.Excel.Application();
_excelApp.Visible = true ;
string fileName = C:\\Users\\kritijain\\Desktop\\SampleFile.xls跨度>;
// 打开工作簿
工作簿工作簿= _excelApp。 Workbooks.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);
// 选择第一张表
工作表工作表=(工作表)workbook.Worksheets [ 1 ];
// 在工作表中查找使用的范围
范围excelRange = worksheet.UsedRange;
// 获取工作表中所有单元格的对象数组(其值)
object [,] valueArray =( object [,])excelRange。 get_Value(
XlRangeValueDataType.xlRangeValueDefault);
// 访问单元格
for ( int row = 1 ; row < = worksheet.UsedRange.Rows.Count; ++ row)
{
for (< span class =code-keyword> int col = 1 ; col < = worksheet.UsedRange.Columns.Count; ++ col)
{
// 访问每个单元格
Debug.Print(valueArray [row,col] .ToString());
TextBox1.Text = valueArray [row,col] .ToString();
}
}
// 清理东西
workbook.Close( false ,Type.Missing,Type.Missing);
Marshal.ReleaseComObject(工作簿);
_excelApp.Quit();
Marshal.FinalReleaseComObject(_excelApp);
3.添加以下参考
< span class =code-keyword>正确点击 您的项目和 go 添加参考。 添加 Microsoft.Office.Interop.Excel 程序集。
包含使用 Microsoft.Office.Interop.Excel; 使使用 汇编。
好在这里看看。
动态读写Excel文件 [ ^ ]
使用ASP.NET中的Excel文件读取Excel文件C# [ ^ ]
http ://csharp.net-informations.com/excel/csharp-read-excel.htm [ ^ ]
它会对你有帮助。
how to display an excel cell value into a text box
解决方案
Suppose you have an Excel File SampleFile.xls which has 1 column with the heading Age and below that there are certain values and you have to read the last value of that column into a textbox then you can follow the below mentioned steps:
1. First Add a Label and Textbox on your aspx page. This is the textbox in which the Excel file value will be displayed.
<asp:label id="Label1" runat="server" text="Age" ></asp:label> <asp:textbox id="TextBox1" runat="server"></asp:textbox>
2. In the code behind file write the following code to get the value in a textbox from excel file.
//create the Application object we can use in the member functions. Microsoft.Office.Interop.Excel.Application _excelApp = new Microsoft.Office.Interop.Excel.Application(); _excelApp.Visible = true; string fileName = "C:\\Users\\kritijain\\Desktop\\SampleFile.xls"; //open the workbook Workbook workbook = _excelApp.Workbooks.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); //select the first sheet Worksheet worksheet = (Worksheet)workbook.Worksheets[1]; //find the used range in worksheet Range excelRange = worksheet.UsedRange; //get an object array of all of the cells in the worksheet (their values) object[,] valueArray = (object[,])excelRange.get_Value( XlRangeValueDataType.xlRangeValueDefault); //access the cells for (int row = 1; row <= worksheet.UsedRange.Rows.Count; ++row) { for (int col = 1; col <= worksheet.UsedRange.Columns.Count; ++col) { //access each cell Debug.Print(valueArray[row, col].ToString()); TextBox1.Text = valueArray[row, col].ToString(); } } //clean up stuffs workbook.Close(false, Type.Missing, Type.Missing); Marshal.ReleaseComObject(workbook); _excelApp.Quit(); Marshal.FinalReleaseComObject(_excelApp);
3. Add the following reference
Right click on your project and go to Add reference. Add the Microsoft.Office.Interop.Excel assembly. Include using Microsoft.Office.Interop.Excel; to make use of assembly.
Well see here.
Read and Write Excel File Dynamically[^]
Read Excel File into DataSet in ASP.NET Using C#[^]
http://csharp.net-informations.com/excel/csharp-read-excel.htm[^]
It will be help you.
这篇关于如何在文本框中显示Excel单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文