Files
harbour-core/harbour/extras/hbxlsxml/xlsxml.prg
Viktor Szakats 3ef1412e9b 2012-07-24 19:27 UTC+0200 Viktor Szakats (harbour syenar.net)
* contrib/hbide/resources/view_docks.png
    % optimized. please do it before uploading images.

  * extras/hbxlsxml/xlsxml.prg
    ! fixed wrong Chr() values in patch 2012-07-22 18:46 UTC+0200
2012-07-24 17:28:47 +00:00

512 lines
15 KiB
Plaintext

/*
* $Id$
*/
/*
* Harbour Project source code:
*
* Copyright 2011 Fausto Di Creddo Trautwein, ftwein@yahoo.com.br
* www - http://www.xharbour.org http://harbour-project.org
*
* Thanks TO Robert F Greer, PHP original version
* http://sourceforge.net/projects/excelwriterxml/
*
* This program is free software; you can redistribute it AND/OR modify
* it under the terms of the GNU General PUBLIC License as published by
* the Free Software Foundation; either version 2, OR( at your option )
* any later version.
*
* This program is distributed IN the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General PUBLIC License FOR more details.
*
* You should have received a copy of the GNU General PUBLIC License
* along WITH this software; see the file COPYING. IF NOT, write TO
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA( OR visit the web site http://www.gnu.org/ ).
*
* As a special exception, the Harbour Project gives permission FOR
* additional uses of the text contained IN its release of Harbour.
*
* The exception is that, IF you link the Harbour libraries WITH other
* files TO produce an executable, this does NOT by itself cause the
* resulting executable TO be covered by the GNU General PUBLIC License.
* Your use of that executable is IN no way restricted on account of
* linking the Harbour library code into it.
*
* This exception does NOT however invalidate any other reasons why
* the executable file might be covered by the GNU General PUBLIC License.
*
* This exception applies only TO the code released by the Harbour
* Project under the name Harbour. IF you copy code FROM other
* Harbour Project OR Free Software Foundation releases into a copy of
* Harbour, as the General PUBLIC License permits, the exception does
* NOT apply TO the code that you add IN this way. TO avoid misleading
* anyone as TO the status of such modified files, you must delete
* this exception notice FROM them.
*
* IF you write modifications of your own FOR Harbour, it is your choice
* whether TO permit this exception TO apply TO your modifications.
* IF you DO NOT wish that, delete this exception notice.
*
*/
/*----------------------------------------------------------------------*/
#include "hbclass.ch"
#include "fileio.ch"
/*----------------------------------------------------------------------*/
CREATE CLASS ExcelWriterXML
DATA styles INIT {}
DATA formatErrors INIT {=>}
DATA sheets INIT {}
DATA lShowErrorSheet INIT .f.
DATA overwriteFile INIT .f.
DATA docFileName
DATA cDocTitle
DATA cDocSubject
DATA cDocAuthor
DATA cDocCreated
DATA cDocManager
DATA cDocCompany
DATA cDocVersion INIT "11.9999"
DATA cError INIT ""
DATA errors INIT .f.
METHOD New( fileName )
METHOD setOverwriteFile( overwrite )
METHOD showErrorSheet( show )
METHOD addError( cFunction, cMessage )
METHOD getDefaultStyle()
METHOD addStyle( id )
METHOD addSheet( id )
METHOD checkSheetID( id )
METHOD checkStyleID( id )
METHOD writeData( target )
METHOD docTitle( title )
METHOD docSubject( subject )
METHOD docAuthor( author )
METHOD docManager( manager )
METHOD docCompany( company )
ENDCLASS
/*----------------------------------------------------------------------*/
METHOD ExcelWriterXML:new( fileName )
LOCAL style
style := ::addStyle( 'DEFAULT' )
style:name( 'Normal' )
style:alignVertical( 'Bottom' )
IF empty( fileName )
fileName := 'file.xml'
ENDIF
::docFileName := fileName
::cDocCreated := DTOS( DATE() ) + 'T' + TIME() + 'Z'
RETURN SELF
/*----------------------------------------------------------------------*/
METHOD ExcelWriterXML:setOverwriteFile( overwrite )
IF ! ( VALTYPE( overwrite ) == "L" )
::overwriteFile := .f.
ELSE
::overwriteFile := overwrite
ENDIF
RETURN NIL
/*----------------------------------------------------------------------*/
METHOD ExcelWriterXML:showErrorSheet( show )
IF ! ( VALTYPE( show ) == "L" )
::lShowErrorSheet := .t.
ELSE
::lShowErrorSheet := show
ENDIF
RETURN NIL
/*----------------------------------------------------------------------*/
METHOD ExcelWriterXML:addError( cFunction, cMessage )
LOCAL tmp
tmp := { 'FUNCTION' => cFunction,;
'MESSAGE' => cMessage }
::formatErrors += tmp
RETURN NIL
/*----------------------------------------------------------------------*/
METHOD ExcelWriterXML:getDefaultStyle()
RETURN ::styles[ 1 ]
/*----------------------------------------------------------------------*/
METHOD ExcelWriterXML:addStyle( id )
LOCAL style
STATIC styleNum := 1
IF alltrim( id ) == ''
id := NIL
ENDIF
IF id == NIL
id := 'CustomStyle' + ALLTRIM( STR( styleNum, 3 ) )
styleNum++
ENDIF
WHILE ! ::checkStyleID( id )
id := 'CustomStyle' + ALLTRIM( STR( styleNum, 3 ) )
styleNum++
ENDDO
style := ExcelWriterXML_Style():new( id )
AADD( ::styles, style )
RETURN style
/*----------------------------------------------------------------------*/
METHOD ExcelWriterXML:addSheet( id )
LOCAL sheet
STATIC sheetNum:= 1
IF id == NIL
id := 'Sheet' + ALLTRIM( STR( sheetNum, 3 ) )
sheetNum++
ENDIF
WHILE ! ::checkSheetID( id )
id := 'Sheet' + ALLTRIM( STR( sheetNum, 3 ) )
sheetNum++
ENDDO
sheet := ExcelWriterXML_Sheet():New( id )
AADD( ::sheets, sheet )
RETURN sheet
/*----------------------------------------------------------------------*/
METHOD ExcelWriterXML:checkSheetID( id )
LOCAL sheet
IF len( ::sheets ) > 0
FOR EACH sheet IN ::sheets
IF id == sheet:getID()
RETURN .f.
ENDIF
NEXT
ELSE
RETURN .t.
ENDIF
RETURN .t.
/*----------------------------------------------------------------------*/
METHOD ExcelWriterXML:checkStyleID( id )
LOCAL style
IF LEN( ::styles ) > 0
FOR EACH style IN ::styles
IF id == style:getID()
RETURN .f.
ENDIF
NEXT
ELSE
RETURN .t.
ENDIF
RETURN .t.
/*----------------------------------------------------------------------*/
METHOD ExcelWriterXML:writeData( target )
LOCAL style, sheet, xml := "", handle, fileExists, format
LOCAL docTitle := ''
LOCAL docSubject := ''
LOCAL docAuthor := ''
LOCAL docCreated := ''
LOCAL docManager := ''
LOCAL docCompany := ''
IF target == NIL
::cError := "Target filename missing!"
::errors := .t.
RETURN .t.
ENDIF
fileExists := hb_fileExists( target )
IF ( fileExists == .t. .AND. ::overwriteFile == .f. )
::cError := target + " exists and overwriteFile is set to false"
::errors := .t.
RETURN .f.
ENDIF
handle := hb_fcreate( target, FC_NORMAL, FO_EXCLUSIVE )
IF handle == -1
::cError := "Not able to open " + target + " for writing"
::errors := .t.
RETURN .f.
ENDIF
IF ::lShowErrorSheet == .t.
format := ::addStyle( "formatErrorsHeader" )
format:setFontBold()
format:bgColor( "red" )
ENDIF
IF ! empty( ::cDocTitle ); docTitle := "<Title>" + OemToHtmlEspecial( ::cDocTitle ) + "</Title>" + HB_OsNewLine(); ENDIF
IF ! empty( ::cDocSubject ); docSubject := "<Subject>" + OemToHtmlEspecial( ::cDocSubject ) + "</Subject>" + HB_OsNewLine(); ENDIF
IF ! empty( ::cDocAuthor ); docAuthor := "<Author>" + OemToHtmlEspecial( ::cDocAuthor ) + "</Author>" + HB_OsNewLine(); ENDIF
IF ! empty( ::cDocCreated ); docCreated := "<Created>" + OemToHtmlEspecial( ::cDocCreated ) + "</Created>" + HB_OsNewLine(); ENDIF
IF ! empty( ::cDocManager ); docManager := "<Manager>" + OemToHtmlEspecial( ::cDocManager ) + "</Manager>" + HB_OsNewLine(); ENDIF
IF ! empty( ::cDocCompany ); docCompany := "<Company>" + OemToHtmlEspecial( ::cDocCompany ) + "</Company>" + HB_OsNewLine(); ENDIF
xml := '<?xml version="1.0"?>' + HB_OsNewLine()
xml += '<?mso-application progid="Excel.Sheet"?>' + HB_OsNewLine()
xml += '<Workbook' + HB_OsNewLine()
xml += 'xmlns="urn:schemas-microsoft-com:office:spreadsheet"' + HB_OsNewLine()
xml += 'xmlns:o="urn:schemas-microsoft-com:office:office"' + HB_OsNewLine()
xml += 'xmlns:x="urn:schemas-microsoft-com:office:excel"' + HB_OsNewLine()
xml += 'xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"' + HB_OsNewLine()
xml += 'xmlns:html="http://www.w3.org/TR/REC-html40">' + HB_OsNewLine()
xml += '<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">' + HB_OsNewLine()
IF ! empty( ::cDocTitle ); xml += ' ' + docTitle ; ENDIF
IF ! empty( ::cDocSubject ); xml += ' ' + docSubject; ENDIF
IF ! empty( ::cDocAuthor ); xml += ' ' + docAuthor ; ENDIF
IF ! empty( ::cDocCreated ); xml += ' ' + docCreated; ENDIF
IF ! empty( ::cDocManager ); xml += ' ' + docManager; ENDIF
IF ! empty( ::cDocCompany ); xml += ' ' + docCompany; ENDIF
xml += ' <Version>' + ::cDocVersion + '</Version>' + HB_OsNewLine()
xml += '</DocumentProperties>' + HB_OsNewLine()
xml += '<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel" />' + HB_OsNewLine()
xml += '<Styles>' + HB_OsNewLine()
fwrite( handle,xml )
xml := ""
FOR EACH style IN ::styles
xml += style:getStyleXML()
NEXT
xml += '</Styles>'+HB_OsNewLine()
fwrite( handle, xml )
xml := ""
IF LEN( ::sheets ) == 0
::addSheet()
ENDIF
IF len( ::sheets ) > 0
FOR EACH sheet IN ::sheets
xml += sheet:getSheetXML( handle )
IF LEN( sheet:getErrors() ) > 0
::errors := .t.
ENDIF
NEXT
ENDIF
IF LEN( ::formatErrors ) > 0
::errors := .t.
ENDIF
xml += '</Workbook>'
fwrite( handle, xml )
xml := ""
fclose( handle )
RETURN .t.
/*----------------------------------------------------------------------*/
METHOD ExcelWriterXML:docTitle( title )
IF HB_ISSTRING( title )
::cDocTitle := title
ENDIF
RETURN NIL
/*----------------------------------------------------------------------*/
METHOD ExcelWriterXML:docSubject( subject )
IF HB_ISSTRING( subject )
::cDocSubject := subject
ENDIF
RETURN NIL
/*----------------------------------------------------------------------*/
METHOD ExcelWriterXML:docAuthor( author )
IF HB_ISSTRING( author )
::cDocAuthor := author
ENDIF
RETURN NIL
/*----------------------------------------------------------------------*/
METHOD ExcelWriterXML:docManager( manager )
IF HB_ISSTRING( manager )
::cDocManager := manager
ENDIF
RETURN NIL
/*----------------------------------------------------------------------*/
METHOD ExcelWriterXML:docCompany( company )
IF HB_ISSTRING( company )
::cDocCompany := company
ENDIF
RETURN NIL
/*----------------------------------------------------------------------*/
FUNCTION AnsiToHtml( x )
RETURN x
/*----------------------------------------------------------------------*/
FUNCTION OEMTOHTML( xtxt )
LOCAL afrm, i, xret:= "", xpos
afrm := {;
{ Chr( 160 ), "&aacute;" },;
{ Chr( 131 ), "&acirc;" },;
{ Chr( 7 ), "&agrave;" },;
{ Chr( 198 ), "&atilde;" },;
{ Chr( 43 ), "&ccedil;" },;
{ Chr( 39 ), "&eacute;" },;
{ Chr( 136 ), "&ecirc;" },;
{ Chr( 161 ), "&iacute;" },;
{ Chr( 162 ), "&oacute;" },;
{ Chr( 147 ), "&ocirc;" },;
{ Chr( 228 ), "&otilde;" },;
{ Chr( 163 ), "&uacute;" },;
{ Chr( 129 ), "&uuml;" },;
{ Chr( 117 ), "&Aacute;" },;
{ Chr( 20 ), "&Acirc;" },;
{ Chr( 7 ), "&Agrave;" },;
{ Chr( 199 ), "&Atilde;" },;
{ Chr( 128 ), "&Ccedil;" },;
{ Chr( 144 ), "&Eacute;" },;
{ Chr( 210 ), "&Ecirc;" },;
{ Chr( 214 ), "&Iacute;" },;
{ Chr( 224 ), "&Oacute;" },;
{ Chr( 226 ), "&Ocirc;" },;
{ Chr( 229 ), "&Otilde;" },;
{ Chr( 233 ), "&Uacute;" },;
{ Chr( 154 ), "&Uuml;" },;
{ Chr( 45 ), "&ndash;" } ;
}
FOR i:= 1 TO LEN( xtxt )
IF( xpos:= ASCAN( afrm, {|x| SUBS( xtxt,i,1 ) == x[1] } ) ) > 0
xret+= afrm[xpos,2]
ELSE
xret+= SUBS( xtxt,i,1 )
ENDIF
NEXT
RETURN xret
/*----------------------------------------------------------------------*/
FUNCTION OEMTOHTMLESPECIAL( xtxt )
LOCAL afrm, i, xret:= "", xpos
xtxt := exretiraAcentos( xtxt )
afrm := {;
{ '&', "&amp;" },;
{ '"', "&quot;" },;
{ "'", "&#039;" },;
{ "<", "&lt;" },;
{ ">", "&gt;" } ;
}
FOR i := 1 TO LEN( xtxt )
IF ( xpos := ASCAN( afrm, {|x| SUBSTR( xtxt, i, 1 ) == x[ 1 ] } ) ) > 0
xret += afrm[xpos,2]
ELSE
xret += SUBSTR( xtxt,i,1 )
ENDIF
NEXT
RETURN xret
/*----------------------------------------------------------------------*/
FUNCTION EXRETIRAACENTOS( xtxt )
LOCAL afrm, i, xret:= "", xpos
afrm := {;
{ Chr( 160 ), "a" },;
{ Chr( 131 ), "a" },;
{ Chr( 7 ), "a" },;
{ Chr( 198 ), "a" },;
{ Chr( 43 ), "c" },;
{ Chr( 39 ), "e" },;
{ Chr( 136 ), "e" },;
{ Chr( 161 ), "i" },;
{ Chr( 162 ), "o" },;
{ Chr( 147 ), "o" },;
{ Chr( 228 ), "o" },;
{ Chr( 163 ), "u" },;
{ Chr( 129 ), "u" },;
{ Chr( 117 ), "A" },;
{ Chr( 20 ), "A" },;
{ Chr( 7 ), "A" },;
{ Chr( 199 ), "A" },;
{ Chr( 128 ), "C" },;
{ Chr( 144 ), "E" },;
{ Chr( 210 ), "E" },;
{ Chr( 214 ), "I" },;
{ Chr( 224 ), "O" },;
{ Chr( 226 ), "O" },;
{ Chr( 229 ), "O" },;
{ Chr( 233 ), "U" },;
{ Chr( 154 ), "U" },;
{ Chr( 166 ), "." },;
{ Chr( 167 ), "." },;
{ Chr( 248 ), "." },;
{ Chr( 141 ), "" } ;
}
FOR i := 1 TO LEN( xtxt )
IF ( xpos:= ASCAN( afrm, { |x| SUBSTR( xtxt, i, 1 ) == x[ 1 ] } ) ) > 0
xret += afrm[ xpos, 2 ]
ELSE
xret += SUBSTR( xtxt, i, 1 )
ENDIF
NEXT
RETURN xret
/*----------------------------------------------------------------------*/