cancel
Showing results for 
Search instead for 
Did you mean: 

Read Excel Sheet and Import the data

Former Member
0 Kudos

Hi,

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

Any code sample out there?

Thank you,

Rune

Accepted Solutions (1)

Accepted Solutions (1)

Nussi
Active Contributor
0 Kudos

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

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi Rune,

Unfortunately I don't have any examples available to me at the moment - but would it be possible to use DTW for this purpose? If you were to reformat the excel sheet to fit the template of DTW you wouldn't have to write any further implementation for this as DTW is designed to do it automatically.

[http://service.sap.com/smb/sbo/dtw|http://service.sap.com/smb/sbo/dtw]

However, if you have already looked into this and decided against it, hopefully one of my fellow posters has a few examples.

Regards,

Niall

SAP Business One