Recent

Author Topic: OLE automation write chinese characters to Excel  (Read 6291 times)

robotech

  • Newbie
  • Posts: 4
OLE automation write chinese characters to Excel
« on: May 03, 2016, 02:06:37 pm »
Dear All,

I'm using Lazarus 1.6 , and try to do the same what i did in Delphi several years before at last:
Fill some cells by OLE office automation.

When i hardcoding the text to the program, it's export very well to Excel, but
if i want to send a variable to excel (for ex: edit1.text) just appear Chinese text, but nothing else...

Here is some code:

procedure TForm1.Button2Click(Sender: TObject);
begin
  XLApp := CreateOleObject('Excel.Application');
 try
   XLApp.Visible := True;
   XLApp.DisplayAlerts := True;
    XLApp.Workbooks.Add;     // Open the Workbook
       XLApp.Cells[1,1].Value :='This is a Hardcoded text in program';
       XLApp.Cells[2,1].Value :=edit2.text ;
      end;
    end;
 finally
   XLApp.ActiveWorkBook.SaveAs(edit1.text, 51);  //here also save excel whith name full chinese chars...

end;           

I took  screenshot about the results....
I think there is must be a string conversation  issue, but a couldn't figure it out what may wrong...      Could you help in this , please?     

---UPDATE---!

Very interesting! if i type:

    XLApp.Cells[2,1].Value :=utf8Decode(edit2.text) ;

Stops with SIGSEGV error, but this is the conversion, what i was looking for...

But if i put in function

Function StrToWidestr(aString: String): WideString;
begin
  Result:= UTF8Decode(aString);
end;                       

with
XLApp.Cells[2,1].Value :=StrToWidestr(edit2.text) ;   

Its works! How this can be?
 

Another question:

Can you help me to find the command to change printer?

Xlapp.Application.ActivePrinter:='Xerox Phaser 3117' doesnt work... or how can i make sure the name of the printer exactly in the system?




« Last Edit: May 03, 2016, 02:27:46 pm by robotech »

fred

  • Full Member
  • ***
  • Posts: 201
Re: OLE automation write chinese characters to Excel
« Reply #1 on: May 03, 2016, 02:43:00 pm »
Did you try WideString(Edit2.Text) ?

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: OLE automation write chinese characters to Excel
« Reply #2 on: May 03, 2016, 02:44:38 pm »
Excel, by OLE automation, I think, requires Unicode strings. Edit2.Text, however, is UTF8, and assigning to a variant should not trigger an internal conversion. So, I think, you should try
Code: Pascal  [Select][+][-]
  1. XLApp.Cells[2,1].Value :=UTF8ToUTF16(edit2.text);
  2. // or
  3. // ... := UTF8Decode(edit2.text);

Alternatively you could try fpspreadsheet which works in UTF8 and does the conversions needed for saving automatically:
Code: Pascal  [Select][+][-]
  1. uses
  2.   fpspreadsheet, fpstypes, xlsxooxml;
  3. var
  4.   workbook: TsWorkbook;
  5.   worksheet: TsWorksheet;
  6. begin
  7.   workbook := TsWorkbook.Create;
  8.   worksheet := workbook.AddWorksheet('Sheet 1');
  9.   worksheet.WriteText(0, 0, 'This is a hardcoded text in program');
  10.   worksheet.WriteText(1, 0, Edit2.Text);
  11.   workbook.WriteToFile(Edit1.Text, sfOOXML, true);
  12.   workbook.Free;
  13. end;

It would be interesint to see it with Chinese characters, I never tested it this way...

As for the printer, try
Code: Pascal  [Select][+][-]
  1. uses
  2.   Printers;
  3. var
  4.   activePrinter: String;
  5. ....
  6.   activePrinter := Printer.PrinterName;
In fpspreadsheet, the Printer, however, is not supported.


robotech

  • Newbie
  • Posts: 4
Re: OLE automation write chinese characters to Excel
« Reply #3 on: May 04, 2016, 10:09:11 am »
I have tried Printers class, but i didnt reach any goal...

everything i try it makes : SIGSEGV error...  but just simple a wanted to ask the installed printers in two different way:


procedure TForm1.Button2Click(Sender: TObject);
begin
  ComboBox1.Items.Assign(Printer.Printers);
end;         


procedure TForm1.Button3Click(Sender: TObject);
begin
  edit1.text := Printer.PrinterName;
end;


Lazarus stopping here in Printers class:

//Return the current selected printer
function TPrinter.GetPrinterIndex: integer;
begin
  Result:=fPrinterIndex;
  if (Result<0) and (Printers.Count>0) then
     Result:=0; //printer by default
end;               

What could be wrong?

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: OLE automation write chinese characters to Excel
« Reply #4 on: May 04, 2016, 11:03:01 am »
Did you add the package printer4lazarus as a requirement to your project? http://wiki.freepascal.org/Using_the_printer

robotech

  • Newbie
  • Posts: 4
Re: OLE automation write chinese characters to Excel
« Reply #5 on: May 04, 2016, 11:09:22 am »
Huhh!  :)

This was a very good observation, Thanks!  Of course i didn't add it!  :D

But i really need to write it onto a sticky notes to remember for this in the following years too, as i don't use printer class to often! 

Thanks for help!

robotech

  • Newbie
  • Posts: 4
Re: OLE automation write chinese characters to Excel
« Reply #6 on: May 04, 2016, 11:42:50 am »

Just one more question:

I intended to know what printers are available, because i would like to print documents on two different printer (PDF, and a real printer).

To set this with OLE autiomation i use:

XLApp : OLEvariant;

....


XLApp.Appication.ActivePrinter:=strtowidestr(edit1.text);

As errormessage shown: Unable to set ActivePrinter property of the Application class

 is that means that this property is not exist, (have to), or it means it is read only, or how i can set printer to the Excel by OLE automation?

Unfortunately if i set printer by lazarus with:

Printer.Setprinter('name');

doesn't work, because when excel opens, it's set printer the default printer...


wp

  • Hero Member
  • *****
  • Posts: 11916
Re: OLE automation write chinese characters to Excel
« Reply #7 on: May 04, 2016, 12:58:08 pm »
Microsoft knows many tricks to confuse people...

You must add the port name to the printer name. Strangely enough, all ports are called "NeXX:" with XX being replaced by a two-digit number, but I did not find out the logics behind these numbers. And, there is a localized word between the printer name and port name to indicate that the printer prints TO this port. Just open Excel, record a macro for printing to the requested port, edit the macro and you'll see the required string in the command "ExecuteExcel4Macro". For selecting the printer "PDF24 Fax" on my Germany system, for example, the code would be
Code: Pascal  [Select][+][-]
  1.   XlApp.Application.ActivePrinter:= WideString(PDF24 Fax auf Ne02:');
  2.  // In English Windows this would be
  3.  // XlApp.Application.ActivePrinter:= WideString(PDF24 Fax to Ne02:');
Tested, and this works.

[EDIT]
Check this out (I didn't), it may help you to find the printer string correctly.
« Last Edit: May 04, 2016, 01:06:35 pm by wp »

 

TinyPortal © 2005-2018