Vishal
Vishal

Reputation: 6368

Read data from excel in asp.net

I want to read a whole excel sheet's data in a single textbox.

Is this possible?

I know the code to read a cell's data from excel to a textbox.

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

Upvotes: 0

Views: 1412

Answers (1)

Sergey Shoshin
Sergey Shoshin

Reputation: 475

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;
        }

This is Range from one cell to another:

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

You also can use xlWorksheet.UsedRange.

Upvotes: 2

Related Questions