Hello Janis!
I have another code. I usually save a csv-file as Excel-File.
My code looks so:
Code |
---|
&AtClient
Procedure SelectFile(Command)
Mode = FileDialogMode.Open;
OpeningFileDialogue = New FileDialog(Mode);
OpeningFileDialogue.FullFileName = "";
Filter = "Excel(*.xlsx)|*.xlsx|Excel 2003(*.xls)|*.xls|Commer(*.dta)|*.dta|Commern(*.dtn)|*.dtn";
OpeningFileDialogue.Filter = Filter;
OpeningFileDialogue.Multiselect = False;
OpeningFileDialogue.Title = "Sel ect file";
If OpeningFileDialogue.Choose() Then
FilesArray = OpeningFileDialogue.SelectedFiles;
For Each FileName In FilesArray Do
Selection = New File(FileName);
EndDo;
ImportAtServer(Selection.FullName);
Else
DoMessageBox("File(s) not selected!");
EndIf;
EndProcedure
|
In this case you do not need to write a fix path to your file, you can select it fr om whatever you want.
Then at Server:
Code |
---|
&AtServer
Procedure ImportAtServer(File)
try
ExcelApp = New COMObject("Excel.Application");
except
Message(ErrorDescription());
Message("Can't initialize Excel");
Return;
EndTry;
try
ExcelFile = ExcelApp.Workbooks.Open(File);
NRows = ExcelApp.Sheets(1).UsedRange.Rows.Count;
For n= 2 To NRows Do
ProductCreated = False;
LineTotal = 0;
OrderID = ExcelApp.Sheets(1).Cells(n,2).Value;
SendingCosts = ExcelApp.Sheets(1).Cells(n,5).Value;
Versandschein = ExcelApp.Sheets(1).Cells(n,8).Value;
SendingDate = ExcelApp.Sheets(1).Cells(n,7).Value;
/// Here do something
EndDo;
Except
Message(ErrorDescription());
ExcelApp.Application.Quit();
EndTry;
Try
ExcelApp.ActiveWorkbook.Close(False);
Except
EndTry;
EndProcedure
|
It starts with n = 2 because the first line in Excel is the header.
For example:
Product code || Product Name - this is the header.
The second line is the product
|| Item 1
|| Item 2
an so on.
Please consider that in 1C:Enterprise the first field in an array is not 0 (zero) like in java or C++ but 1(one).
The first column (Product code) let just empty because the platform will give automatic code but you can also use your codes.
Then it is somehow:
Code |
---|
NewProduct = Catalogs.Products.CreateItem();
////Do not forget to write the item after creating.
NewProduct.Write();
|