Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Read Excel Sheet and Import the data

Hi,

I am going to read a Excel Sheet and import the data in Sales Order?

Any code sample out there?

Thank you,

Rune

replied

Hi Rune,

i have a c# sample here that shows you how to open the excel and access a given cell.

Short description:

- i open a file named Import.xls

- usually you don't need to change the other parameters in the Workbooks.Open

- what you have to do is to add Excel Library and/or Office Libary to your references.

- the logic down shows you how to access a single cell, put the code in a loop

Excel.Application excelApp = new Excel.ApplicationClass();

Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(System.IO.Directory.GetParent(System.Windows.Forms.Application.ExecutablePath).ToString() + "\\Import.xls", 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelWorkbook.Worksheets[1];
Excel.Range excelCell = null;

int CellCounter = 1;
string Result = "";

// Get Cell A1
excelCell = (Excel.Range)excelWorksheet.get_Range("A" + CellCounter.ToString(), "A" + CellCounter.ToString());


if (excelCell.Value2.ToString() != null)
{			
    Result = excelCell.Value2.ToString();
}

CellCounter++;

// Get Cell B1
excelCell = (Excel.Range)excelWorksheet.get_Range("B" + CellCounter.ToString(), "B" + CellCounter.ToString());

if (excelCell.Value2.ToString() != null)
{			
    Result = excelCell.Value2.ToString();
}

try the code once with a normal excel.

i hope it helps you

lg David

0 View this answer in context

Helpful Answer

by
Not what you were looking for? View more on this topic or Ask a question