Home > Back-end >  SaveDBGridEhToExportFile export excel
SaveDBGridEhToExportFile export excel

Time:10-01

After using saveDBGridEhToExportFile export excel as a field of more than 135 Chinese characters Chinese characters, when you open the excel file times lost data error, opened more than 135 Chinese characters of the content of the field unit is empty, the other fields unit normal, consult the warrior are how to solve this problem,

CodePudding user response:

What are the master encountered similar problems,

CodePudding user response:

Excel problems or DBGRID, updated version a try,

CodePudding user response:

Excel should be no problem, more than 135 Chinese characters in direct copy is allowed

CodePudding user response:

I also encountered this kind of problem, is a database field varchar type more than 255 characters, export to Excel will no content, Eh version is 5.6, is very difficult,

CodePudding user response:

Who has a good way to solve this problem?

CodePudding user response:

Borrow to ask saveDBGridEhToExportFile export excel file of the multilayer header can do it now? Effect how?

CodePudding user response:

He data types may be AnsiString, may be not more than 255 bytes

CodePudding user response:

Their direct export,
//adopt BIFF8 format specification binary stream into the XLS file 
The function sycExportDateToExcel (ASender: TDBGridEh; Var TitleName: string) : Boolean;
Procedure XLSWriteStringCell (nRow, nCol: WORD; AValue: String);//write string data
Var: L Word;
Var _str: AnsiString;
The begin
_str:=AnsiString (AValue);//casts, compatible Delphi2009, USES the type String to the code, code under Delphi2007 trip, there is no
L:=Length (_str);//Length returns the number of characters, use the AnsiString is bytes
ArXlsString [1] :=8 + L;
ArXlsString [2] :=nRow;
ArXlsString [3] :=nCol;
ArXlsString [5] :=L;
AFileStream. WriteBuffer (arXlsString, SizeOf (arXlsString));
AFileStream. WriteBuffer (Pointer (_str) ^, L);//bytes
end;

Procedure XLSWriteIntegerCell (nRow, nCol: WORD; AValue: Integer);//write integer
Var V: DWORD;
The begin
ArXlsInteger [2] :=nRow;
ArXlsInteger [3] :=nCol;
AFileStream. WriteBuffer (arXlsInteger, SizeOf (arXlsInteger));
V:=(AValue SHL 2) or 2;
AFileStream. WriteBuffer (V, 4);
end;

Procedure XLSWriteFloatCell (nRow, nCol: WORD; AValue: Double);//write floating-point
The begin
ArXlsNumber [2] :=nRow;
ArXlsNumber [3] :=nCol;
AFileStream. WriteBuffer (arXlsNumber, SizeOf (arXlsNumber));
AFileStream. WriteBuffer (AValue, 8);
end;
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- write embedded cell function end -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
The begin
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the main function of body began to -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
//cell records BIFF8 definition, so use the code to initialize the array, is green, in order to function,,
ArXlsBegin [0] :=$809; ArXlsBegin [1] :=8; ArXlsBegin [2] :=0; ArXlsBegin [3] :=$10; ArXlsBegin [4] :=0; ArXlsBegin [5] :=0;
ArXlsEnd [0] :=$0 a; ArXlsEnd [1] :=0;
ArXlsString [0] :=$204; ArXlsString [1] :=0; ArXlsString [2] :=0; ArXlsString [3] :=0; ArXlsString [4] :=0; ArXlsString [5] :=0;
ArXlsNumber [0] :=$203; ArXlsNumber [1] :=14; ArXlsNumber [2] :=0; ArXlsNumber [3] :=0; ArXlsNumber [4] :=0;
ArXlsInteger [0] : e=$27; ArXlsInteger [1] :=10; ArXlsInteger [2] :=0; ArXlsInteger [3] :=0; ArXlsInteger [4] :=0;
ArXlsBlank [0] :=$201; ArXlsBlank [1] :=6; ArXlsBlank [2] :=0; ArXlsBlank [3] :=0; ArXlsBlank [4] :=$17;

//the DBGrid data export to Excel table
Result:=False;
If ASender=nil then
Exit;
If not Assigned (ASender) then
Exit;//data table undistributed
If ASender. Columns. The Count=0 then
Exit;//DBGrid has no fixed column
If not Assigned (ASender. The DataSource) then
Exit;//the data source table undistributed
If not Assigned (ASender. The DataSource. The DataSet) then
Exit;//data set undistributed
If not ASender. The DataSource. The DataSet. The Active then
Exit;//data set did not open the
If ASender. The DataSource. The DataSet. The RecordCount=0 then
Exit;//data sets no record
If ASender. The DataSource. The DataSet. RecordCount> Then 65535
The begin
If Application. MessageBox (' due to the amount of data is too much, Excel spreadsheets can only hold the front article 65535 data, unable to export information, after confirm to export! ', 'prompt', MB_ICONQUESTION or MB_YESNO) & lt;> MrYes then
Exit;//users give up export
end;

ASaveDlg:=TSaveDialog. Create (nil);
ASaveDlg. FileName:=TitleName + + FormatDateTime '_' (' YYMMDD, Date);
ASaveDlg. Title:='type in the name of the Excel spreadsheet file export data'.
ASaveDlg. Filter:='Microsoft Excel spreadsheet file | *. XLS';
ASaveDlg. DefaultExt:='XLS';
ASaveDlg. Options:=[ofHideReadOnly, ofFileMustExist ofPathMustExist];
If not aSaveDlg. Execute then
Begin//note that this dialog box, users may change the program of "the current folder" position, the function after the meeting, in order to process more robust, it is recommended that the reset handle position of the current folder
ASaveDlg. Free;
Exit;//file open failed or user to cancel
end;
XlsFileName:=aSaveDlg. FileName;//save file name
ASaveDlg. Free;

If FileExists (xlsFileName) then
The DeleteFile (PAnsiChar (xlsFileName));//if the export file exist, first remove

AFileStream:=TFileStream. Create (xlsFileName fmCreate);
ASender. The DataSource. The DataSet. DisableControls;
Try
//write file header
AFileStream. WriteBuffer (arXlsBegin, SizeOf (arXlsBegin));

Col:=0; Row:=0;//XSL form coordinate variables in
//write head, to row 0
For I:=0 to ASender. Columns. Do the Count - 1
The begin//export only the title of the selected column, and the column is visible, and column headings no strikethrough font style
XLSWriteStringCell (Row, Col, ASender. Columns. The Items [I] Title. The Caption);
Inc (Col);
end;

//write the data set of data, from line 1
Row:=1; Col:=0;
ABookMark:=ASender DataSource. The DataSet. GetBookmark;//save data sets export the cursor position before the
ASender. The DataSource. The DataSet. First;
While not ASender. The DataSource. The DataSet. Eof do
The begin
For I:=0 to ASender. Columns. Do the Count - 1
The begin
If Assigned (ASender Columns [I] Field) then
The begin
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related