Solved

Insert a line in a Excel file

Posted on 2001-09-18
3
711 Views
Last Modified: 2010-04-04
I'm using Delphi 6 and TExcelApplication and TExcelWorksheet.  I want to insert a line between two existing lines in an Excel file. ( Same thing as, in Excel, rigth-clicking and select "Insert". )
I can insert a new line at the end, but I want to insert between two line.  How to do that ?
Thanks.
0
Comment
Question by:frdep
  • 2
3 Comments
 
LVL 17

Accepted Solution

by:
inthe earned 200 total points
ID: 6491917
Hi frdep,
for an example (where WS is your worksheet) you can do this to move the rows down and insert some text at row A3:

begin
WS.Range['A3','A3'].EntireRow.Insert(TOleEnum(xlShiftDown));
WS.Range['A3','A3'].Value := 'HELLO IM SOME INSERTED TEXT';
end;

Regards Barry
0
 

Author Comment

by:frdep
ID: 6493551
Thanks.
How Do I do to keep the same format for the cells ?
0
 
LVL 17

Expert Comment

by:inthe
ID: 6494270
hi,
i couldnt work out how to read the current format to save it in a var and reapply it after the insert but if you know it already you can do like so and either format the whole sheet the same or just format your newly inserted row:


Uses ActiveX;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ExcelWorksheet1.ConnectTo(ExcelApplication1.ActiveSheet as _Worksheet);
  ExcelWorksheet1.Range['A2', 'A2'].EntireRow.Insert(TOleEnum(xlShiftDown));
  ExcelWorksheet1.Range['A2','A2'].Value := 'HELLO IM SOME INSERTED TEXT';
   // this will just format all cells in the sheet the same
  //  ExcelWorksheet1.Cells.Select;
  //  ExcelWorksheet1.Cells.Font.Name:='Arial Cyr';
  //  ExcelWorksheet1.Cells.Font.Color := clgreen;
  //  ExcelWorksheet1.cells.Font.Size:=9;
  // or below will just format the new row
  ExcelWorksheet1.Range['A2','A2'].Font.Name:='Arial Cyr';
  ExcelWorksheet1.Range['A2','A2'].Font.Color := clblue;
  ExcelWorksheet1.Range['A2','A2'].Font.Size:=9;
end;
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question