从asp.net中的excel读取数据

时间:2013-05-02 21:02:15

标签: asp.net vb.net excel textbox

我想在一个文本框中阅读整个Excel工作表的数据。

这可能吗?

我知道将单元格数据从excel读取到文本框的代码。

PrivateSub Button18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button18.Click

    Dim xlaTest As Excel.Application = CType(CreateObject("Excel.Application"), Excel.Application)
    xlaTest.Visible = True
    Dim wbkTest As Excel.Workbook = xlaTest.Workbooks.Open("D:\Tests\VBTrials\ExcelTests\TestWorkbook.xls")
    Dim wshTest As Excel.Worksheet = DirectCast(wbkTest.Worksheets("Sheet1"), Excel.Worksheet)
    TextBox1.Text = wshTest.Range("B2").Value
    wbkTest.Close()
    xlaTest.Quit()

EndSub

1 个答案:

答案 0 :(得分:2)

private List<object> Excelvoider()
        {
            Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"C:/Temp/Книга1.xlsx");
            Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
            Excel.Range xlRange = xlWorksheet.Range["B3", "Q58"];
            List<object> stroke = new List<object>();
            int rowCount = xlRange.Rows.Count;
            int colCount = xlRange.Columns.Count;
            for (int i = 1; i <= rowCount; i++)
            {
                for (int j = 1; j <= colCount; i++)
                {
                      stroke.Add(xlRange.Value2[i, j]);
                }
            }
            return stroke;
        }

这是从一个单元格到另一个单元格的范围:

Excel.Range xlRange = xlWorksheet.Range["B3", "Q58"];

您也可以使用xlWorksheet.UsedRange。