 |
VFP 愛用者社區 本討論區為 Visual Foxpro 愛用者經驗交流的地方, 請多多利用"搜尋"的功能, 先查看看有無前例可循, 如果還有不懂的再發問. 部份主題有附加檔案, 須先註冊成為社區居民才可以下載.
|
上一篇主題 :: 下一篇主題 |
發表人 |
內容 |
goto-dream
註冊時間: 2004-05-11 文章: 909
第 1 樓
|
發表於: 星期二 八月 24, 2010 12:23 pm 文章主題: 分享oo.用vfp |
|
|
分享ooo這不是我寫的
原作者可以在網路上找到
買excel要$$.幫客戶裝oo
openoffices + foxpro
代碼: | **PROCEDURE VfpOOoCalcExample()
* Create a spreadsheet.
PARAMETERS f_table
WAIT_window('正在轉換,請稍候。。。')
SELECT (f_table)
nCount=Reccount()
LOCAL oDoc
oDoc = OOoOpenURL( "private:factory/scalc" )
* Get first sheet
LOCAL oSheet
oSheet = oDoc.getSheets().getByIndex( 0 )
** 陔崝 2006/04/24
SELECT (f_table)
&& 梓枙
oSheet.getCellByPosition( 0, 0 ).setString( "正在導入,可能時間較長,請耐心等候..... (完成後此提示自動消除)")
FOR i=1 TO FCOUNT()
cField_name=FIELD(i)
oSheet.getCellByPosition( i-1, 1 ).setString( cField_name)
ENDFOR
m=2
SCAN
FOR i=1 TO FCOUNT()
cField_name=FIELD(i)
Field_value=&cField_name.
IF UPPER(TYPE(cField_name))='N'
oSheet.getCellByPosition( i-1, m ).setValue( IIF(ISNULL(Field_value) or EMPTY(Field_value),0,Field_value))
ELSE
oSheet.getCellByPosition( i-1, m ).setString( IIF(ISNULL(Field_value) or EMPTY(Field_value),'',Field_value))
ENDIF
ENDFOR
oSheet.getCellByPosition( 0, 0 ).setString( "正在導入,可能時間較長,請耐心等候..... (完成後此提示自動消除) 還剩 : "+ALLTRIM(STR(nCount-1))+" 行記錄")
m=m+1
nCount=nCount-1
ENDSCAN
oSheet.getCellByPosition( 0, 0 ).setString( "")
WAIT clear
RELEASE oSheet,oDoc
*-----
* Put some sales figures onto the sheet.
* only test
**oSheet.getCellByPosition( 0, 0 ).setString( "Month" )
**oSheet.getCellByPosition( 1, 0 ).setString( "Sales" )
**oSheet.getCellByPosition( 2, 0 ).setString( "End Date" )
**oSheet.getCellByPosition( 0, 1 ).setString( "Jan" )
**oSheet.getCellByPosition( 0, 2 ).setString( "Feb" )
**oSheet.getCellByPosition( 0, 3 ).setString( "Mar" )
**oSheet.getCellByPosition( 0, 4 ).setString( "Apr" )
**oSheet.getCellByPosition( 0, 5 ).setString( "May" )
**oSheet.getCellByPosition( 0, 6 ).setString( "Jun" )
**oSheet.getCellByPosition( 0, 7 ).setString( "Jul" )
**oSheet.getCellByPosition( 0, 8 ).setString( "Aug" )
**oSheet.getCellByPosition( 0, 9 ).setString( "Sep" )
**oSheet.getCellByPosition( 0, 10 ).setString( "Oct" )
**oSheet.getCellByPosition( 0, 11 ).setString( "Nov" )
**oSheet.getCellByPosition( 0, 12 ).setString( "Dec" )
**oSheet.getCellByPosition( 1, 1 ).setValue( 3826.37 )
**oSheet.getCellByPosition( 1, 2 ).setValue( 3504.21 )
**oSheet.getCellByPosition( 1, 3 ).setValue( 2961.45 )
**oSheet.getCellByPosition( 1, 4 ).setValue( 2504.12 )
**oSheet.getCellByPosition( 1, 5 ).setValue( 2713.98 )
**oSheet.getCellByPosition( 1, 6 ).setValue( 2448.17 )
**oSheet.getCellByPosition( 1, 7 ).setValue( 1802.13 )
**oSheet.getCellByPosition( 1, 8 ).setValue( 2203.22 )
**oSheet.getCellByPosition( 1, 9 ).setValue( 1502.54 )
**oSheet.getCellByPosition( 1, 10 ).setValue( 1207.68 )
**oSheet.getCellByPosition( 1, 11 ).setValue( 1819.71 )
**oSheet.getCellByPosition( 1, 12 ).setValue( 986.03 )
**oSheet.getCellByPosition( 2, 1 ).setFormula( "=DATE(2004;01;31)" )
**oSheet.getCellByPosition( 2, 2 ).setFormula( "=DATE(2004;02;29)" )
**oSheet.getCellByPosition( 2, 3 ).setFormula( "=DATE(2004;03;31)" )
**oSheet.getCellByPosition( 2, 4 ).setFormula( "=DATE(2004;04;30)" )
**oSheet.getCellByPosition( 2, 5 ).setFormula( "=DATE(2004;05;31)" )
**oSheet.getCellByPosition( 2, 6 ).setFormula( "=DATE(2004;06;30)" )
**oSheet.getCellByPosition( 2, 7 ).setFormula( "=DATE(2004;07;31)" )
**oSheet.getCellByPosition( 2, 8 ).setFormula( "=DATE(2004;08;31)" )
**oSheet.getCellByPosition( 2, 9 ).setFormula( "=DATE(2004;09;30)" )
* Note that these last three dates are not set as DATE() function calls.
**oSheet.getCellByPosition( 2, 10 ).setFormula( "10/31/2004" )
**oSheet.getCellByPosition( 2, 11 ).setFormula( "11/30/2004" )
**oSheet.getCellRangeByName( "C13" ).setFormula( "12/31/2004" )
*-----
* Format the date cells as dates.
**oFormats = oDoc.getNumberFormats()
**oLocale = OOoCreateStruct( "com.sun.star.lang.Locale" )
* com.sun.star.util.NumberFormat.DATE = 2
**nDateKey = oFormats.getStandardFormat( 2, oLocale )
**oCell = oSheet.getCellRangeByName( "C2:C13" )
**oCell.NumberFormat = nDateKey
LOCAL ARRAY aOneArg[1]
LOCAL cFile, cURL
* cFile = GetDesktopFolderPathname()+"example"
**cFile = "c:\example"
* Now save the spreadsheet.
**cURL = OOoConvertToURL( cFile + ".ods" )
**aOneArg[1] = OOoMakePropertyValue( "Overwrite", .T. )
**oDoc.storeToUrl( cURL, @ aOneArg )
* Now save it as Excel
**cURL = OOoConvertToURL( cFile + ".xls" )
**aOneArg[1] = OOoMakePropertyValue( "FilterName", "MS Excel 97" )
**oDoc.storeToUrl( cURL, @ aOneArg )
* Close the document.
** oDoc.close( 1 ) && TRUE
**ENDPROC
* Open or Create a document from it's URL.
* New documents are created by URL's such as:
* private:factory/sdraw
* private:factory/swriter
* private:factory/scalc
* private:factory/simpress
FUNCTION OOoOpenURL( cURL )
* LOCAL oPropertyValue
* oPropertyValue = OOoCreateStruct( "com.sun.star.beans.PropertyValue" )
* LOCAL ARRAY aNoArgs[1]
* aNoArgs[1] = oPropertyValue
* aNoArgs[1].Name = "ReadOnly"
* aNoArgs[1].Value = .F.
* These two lines replace the alternate version above,
* which are left commented for the insight they provide.
LOCAL ARRAY aNoArgs[1]
aNoArgs[1] = OOoMakePropertyValue( "Hidden", .F. )
LOCAL oDesktop
oDesktop = OOoGetDesktop()
LOCAL oDoc
oDoc = oDesktop.LoadComponentFromUrl( cURL, "_blank", 0, @ aNoargs )
* Make sure that arrays passed to this document are passed zero based.
COMARRAY( oDoc, 10 )
RETURN oDoc
ENDFUNC
* Create a com.sun.star.beans.PropertyValue struct and return it.
FUNCTION OOoMakePropertyValue( cName, uValue, nHandle, nState )
LOCAL oPropertyValue
oPropertyValue = OOoCreateStruct( "com.sun.star.beans.PropertyValue" )
oPropertyValue.Name = cName
oPropertyValue.Value = uValue
IF TYPE([nHandle])="N"
oPropertyValue.Handle = nHandle
ENDIF
IF TYPE([nState])="N"
oPropertyValue.State = nState
ENDIF
RETURN oPropertyValue
ENDFUNC
* Sugar coated routine to create any UNO struct.
* Use the Bridge_GetStruct() feature of the OLE-UNO bridge.
FUNCTION OOoCreateStruct( cTypeName )
LOCAL oServiceManager
oServiceManager = OOoGetServiceManager()
LOCAL oStruct
oStruct = .NULL.
LOCAL cOldErrHandler
cOldErrHandler = ON( "ERROR" )
ON ERROR = DoNothing__ErrorHandler( ERROR(), MESSAGE(), LINENO(), SYS(16), PROGRAM(), SYS(2018) )
oStruct = oServiceManager.Bridge_GetStruct( cTypeName )
ON ERROR &cOldErrHandler
IF ISNULL( oStruct )
=__OOoReleaseCachedVars()
oServiceManager = OOoGetServiceManager()
oStruct = oServiceManager.Bridge_GetStruct( cTypeName )
ENDIF
RETURN oStruct
ENDFUNC
* Return the OpenOffice.org desktop object.
* Cache it in a global variable.
* Create it if not already cached.
FUNCTION OOoGetDesktop()
IF (TYPE([goOOoDesktop])!="O") OR ISNULL( goOOoDesktop )
PUBLIC goOOoDesktop
goOOoDesktop = OOoServiceManager_CreateInstance( "com.sun.star.frame.Desktop" )
COMARRAY( goOOoDesktop, 10 )
ENDIF
RETURN goOOoDesktop
ENDFUNC
* Return the OpenOffice.org service manager object.
* Cache it in a global variable.
* Create it if not already cached.
FUNCTION OOoGetServiceManager()
IF (TYPE([goOOoServiceManager])!="O") OR ISNULL( goOOoServiceManager )
PUBLIC goOOoServiceManager
goOOoServiceManager = CREATEOBJECT( "com.sun.star.ServiceManager" )
ENDIF
RETURN goOOoServiceManager
ENDFUNC
* Sugar coated routine to ask the service manager to
* create you an instance of some other OpenOffice.org UNO object.
FUNCTION OOoServiceManager_CreateInstance( cServiceName )
LOCAL oServiceManager
oServiceManager = OOoGetServiceManager()
LOCAL oInstance
oInstance = .NULL.
LOCAL cOldErrHandler
cOldErrHandler = ON( "ERROR" )
ON ERROR = DoNothing__ErrorHandler( ERROR(), MESSAGE(), LINENO(), SYS(16), PROGRAM(), SYS(2018) )
oInstance = oServiceManager.createInstance( cServiceName )
ON ERROR &cOldErrHandler
IF ISNULL( oInstance )
=__OOoReleaseCachedVars()
oServiceManager = OOoGetServiceManager()
oInstance = oServiceManager.createInstance( cServiceName )
ENDIF
RETURN oInstance
ENDFUNC
PROCEDURE DoNothing__ErrorHandler( pnError, pcErrMessage, pnLineNo, pcProgramFileSys16, pcProgram, pcErrorParamSys2018 )
ENDPROC
PROCEDURE __OOoReleaseCachedVars()
RELEASE goOOoServiceManager, goOOoDesktop, goOOoCoreReflection
ENDPROC
* Convert a local filename to an OOo URL.
FUNCTION OOoConvertToURL( cFilename )
* Ensure lea* slash.
IF LEFT( cFilename, 1 ) != "/"
cFileName = "/" + cFileName
ENDIF
LOCAL cURL
cURL = CHRTRAN( cFilename, "\", "/" ) && change backslashes to forward slashes.
cURL = "file://" + cURL
RETURN cURL
ENDFUNC
* |
*The function VfpOOoCalcExample() is actually the heart of the example.
*Everything else after VfpOOoCalcExample() is just stuff ripped from my general purpose OOo Library in VFP.
*This was tested in VFP 8 and VFP 7. It should work in VFP 6, but not tested. It may work in VFP 5, but I have reason to believe that it might not. It will definitely not work in VFP 3[/code] _________________ 福隆昌淨水有限公司--淨水器的專家,淨水器,飲水機,濾心!!
想了解更多,您可上幸福雞湯組.找尋!!丁澐瑄.老師.
愛作夢 |
|
回頂端 |
|
 |
ckp6250
註冊時間: 2004-07-30 文章: 1645
第 2 樓
|
發表於: 星期二 八月 24, 2010 4:58 pm 文章主題: |
|
|
感謝分享! |
|
回頂端 |
|
 |
saint
註冊時間: 2003-07-14 文章: 211
第 3 樓
|
發表於: 星期三 八月 25, 2010 3:23 pm 文章主題: |
|
|
其實如果只是簡單產生出excel 中用的表格,不妨可以利用 html table 的方式來達成。 |
|
回頂端 |
|
 |
tsiao0216
註冊時間: 2010-09-20 文章: 1
第 4 樓
|
發表於: 星期一 九月 20, 2010 7:27 pm 文章主題: |
|
|
感謝分享...
對於新手的我...受益匪淺...
想請問一個問題...
如果我想耀開啟一個Excel檔案(已編輯固定格式的檔案)...
假設為 c:\abc.xls
最簡單的寫法應該是如何?  |
|
回頂端 |
|
 |
fpchin
註冊時間: 2004-06-17 文章: 15 來自: Malaysia
第 5 樓
|
發表於: 星期二 九月 21, 2010 10:28 pm 文章主題: |
|
|
試試看這個:
IMPORT FROM c:\abc.xls TYPE XL8 |
|
回頂端 |
|
 |
|
|
您 無法 在這個版面發表文章 您 無法 在這個版面回覆文章 您 無法 在這個版面編輯文章 您 無法 在這個版面刪除文章 您 無法 在這個版面進行投票 您 無法 在這個版面附加檔案 您 無法 在這個版面下載檔案
|
|