생활
델파이에서 StringGrid에서 엑셀전환 방법 알려주세요
StringGrid 에 있는 데이터들을
그대로 엑셀전환하고싶은데
구글링 찾아봐도 이해를 못하겠네요
예제를 통해서 알려주시면 감사하겠습니다
1개의 답변이 있어요!
제 프로그램에 있는 엑셀 전환 프로시저를 그대로 예제로 쓰겠습니다.
procedure TForm.SendDataToExcel;
const
xlVAlignCenter = -4108;
xlHAlignCenter = -4108;
xlThin = 2;
xlEdgeBottom = 9;
xlDouble = -4119;
xlThick = 4;
xl3DColumn = -4100;
xlColumns = 2;
xlLocationAsObject = 2;
var
oXL, oWB, oSheet, oRng, VArray : Variant;
I, J : integer;
nRow, nLastRow, nFirstRow, nDataRow : integer;
nCol : integer;
sDateBegin, sDateEnd, sTemp : string;
bTemp : boolean;
begin
VArray := VarArrayCreate([1, StringGrid1.RowCount, 1, StringGrid1.ColCount], varVariant);
for I := 1 to StringGrid1.RowCount do
begin
for J := 1 to StringGrid1.ColCount do
begin
VArray[i, j] := StringGrid1.cells[j - 1, i - 1];
end;
end;
try
oXL := CreateOleObject('Excel.Application');
except
on e : exception do
begin
Application.MessageBox('MicroSoft Excel 혹은 Office 프로그램이 설치되어 있지 않습니다.', 'Excel 오류', MB_OK);
Exit;
end;
end;
oXL.Visible := false;
oWB := oXL.Workbooks.Add;
oSheet := oWB.ActiveSheet;
nRow := 1;
nCol := 1;
oSheet.Cells[nRow, 1] := '제목';
oRng := oSheet.Range[oSheet.Cells[nRow, 1],
oSheet.Cells[nRow, StringGrid1.ColCount]];
oSheet.Range['A1:K1'].MergeCells:= True;
oSheet.Range['A1:K1'].Font.Bold := true;
oSheet.Range['A1:K1'].Font.Size := 20;
oSheet.Range['A1:K1'].EntireColumn.AutoFit;
oSheet.Range['A1:K1'].EntireRow.AutoFit;
oSheet.Range['A1:K1'].VerticalAlignment := xlVAlignCenter;
oSheet.Range['A1:K1'].HorizontalAlignment := xlHAlignCenter;
nRow := nRow + 2;
oSheet.Cells[nRow, 1] := Label1.Caption;
oRng := oSheet.Range[oSheet.Cells[nRow, 1],
oSheet.Cells[nRow, StringGrid1.ColCount]];
oSheet.Range['A3:K3'].MergeCells:= True;
oSheet.Range['A3:K3'].Font.Bold := true;
oSheet.Range['A3:K3'].Font.Size := 14;
oSheet.Range['A3:K3'].EntireColumn.AutoFit;
oSheet.Range['A3:K3'].EntireRow.AutoFit;
oSheet.Range['A3:K3'].VerticalAlignment := xlVAlignCenter;
oSheet.Range['A3:K3'].HorizontalAlignment := xlHAlignLeft;
nRow := nRow + 2;
oRng := oSheet.Range[oSheet.Cells[nRow, 1],
oSheet.Cells[StringGrid1.RowCount + nRow - 1, StringGrid1.ColCount]];
oRng.Value := VArray;
oRng.Borders.Weight := xlThin;
oRng.EntireColumn.AutoFit;
oRng.EntireRow.AutoFit;
oRng.VerticalAlignment := xlVAlignCenter;
oRng.HorizontalAlignment := xlHAlignCenter;
oRng := oSheet.Range[oSheet.Cells[nRow, 1],
oSheet.Cells[nRow, StringGrid1.ColCount]];
oRng.Interior.ColorIndex := 36;
oRng.Font.Bold := true;
oXL.Visible := true;
oXL.UserControl := true;
end;
혹시나 구동하다가 문제 생기면 댓글 달아주세요^^