明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1024|回复: 2

[讨论] 转一个意大利人弄的代码

[复制链接]
发表于 2016-3-23 12:33 | 显示全部楼层 |阅读模式
程序很屌,可惜不支持中文,哪位大神优化一下
http://www.redchar.net/?x=entry:entry160125-131940
点这里进入源网址

  1. ; FODS Writer
  2. ; Versione : 1.1.1
  3. ; Autore : Roberto Rossi
  4. ; Web : http://www.redchar.net
  5. ;
  6. ; questa libreria di funzioni consente la scrittura di un file FODS
  7. ;
  8. ; il funzionamento ?semplicissimo e consta di 3 passaggi:
  9. ; - prima di tutto occorre creare il documento
  10. ; - poi si possono aggiungere le singole celle
  11. ; - infine si pu?salvare
  12. ;

  13. (setq FODS-FILE-HEADER
  14. "<?xml version=\"1.0\" encoding=\"utf-8\"?>
  15. <office:document xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
  16. xmlns:style=\"urn:oasis:names:tc:opendocument:xmlns:style:1.0\"
  17. xmlns:text=\"urn:oasis:names:tc:opendocument:xmlns:text:1.0\"
  18. xmlns:table=\"urn:oasis:names:tc:opendocument:xmlns:table:1.0\"
  19. xmlns:draw=\"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0\"
  20. xmlns:fo=\"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0\"
  21. xmlns:xlink=\"http://www.w3.org/1999/xlink\"
  22. xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
  23. xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
  24. xmlns:number=\"urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0\"
  25. xmlns:presentation=\"urn:oasis:names:tc:opendocument:xmlns:presentation:1.0\"
  26. xmlns:svg=\"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0\"
  27. xmlns:chart=\"urn:oasis:names:tc:opendocument:xmlns:chart:1.0\"
  28. xmlns:dr3d=\"urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0\"
  29. xmlns:math=\"http://www.w3.org/1998/Math/MathML\"
  30. xmlns:form=\"urn:oasis:names:tc:opendocument:xmlns:form:1.0\"
  31. xmlns:script=\"urn:oasis:names:tc:opendocument:xmlns:script:1.0\"
  32. xmlns:config=\"urn:oasis:names:tc:opendocument:xmlns:config:1.0\"
  33. xmlns:ooo=\"http://openoffice.org/2004/office\"
  34. xmlns:ooow=\"http://openoffice.org/2004/writer\"
  35. xmlns:oooc=\"http://openoffice.org/2004/calc\"
  36. xmlns:dom=\"http://www.w3.org/2001/xml-events\"
  37. xmlns:xforms=\"http://www.w3.org/2002/xforms\"
  38. xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
  39. xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
  40. xmlns:rpt=\"http://openoffice.org/2005/report\"
  41. xmlns:of=\"urn:oasis:names:tc:opendocument:xmlns:of:1.2\"
  42. xmlns:xhtml=\"http://www.w3.org/1999/xhtml\"
  43. xmlns:grddl=\"http://www.w3.org/2003/g/data-view#\"
  44. xmlns:tableooo=\"http://openoffice.org/2009/table\"
  45. xmlns:drawooo=\"http://openoffice.org/2010/draw\"
  46. xmlns:calcext=\"urn:org:documentfoundation:names:experimental:calc:xmlns:calcext:1.0\"
  47. xmlns:loext=\"urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0\"
  48. xmlns:field=\"urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0\"
  49. xmlns:formx=\"urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0\"
  50. xmlns:css3t=\"http://www.w3.org/TR/css3-text/\" office:version=\"1.2\"
  51. office:mimetype=\"application/vnd.oasis.opendocument.spreadsheet\">")


  52. ;funzione che ritorna il contenuto di una cella函数返回一个单元格的内容
  53. (defun FODSGetCellValue (objDoc sheetName row column / result lstdef typeVal)
  54.     (if (= (type row) 'INT)
  55.         (setq row (itoa row))
  56.     )
  57.     (setq column (strcase column))
  58.     (setq typeVal (strcat "cell=" row ":" column "\t" sheetName))
  59.    
  60.     (setq result (assoc typeVal objDoc))
  61.     (if result
  62.         (progn
  63.             (setq result (cadr result))
  64.         )
  65.     )
  66. result
  67. )

  68. ;funzione che rigorna la lettera pi?alta usata per un dato Sheet
  69. (defun FODSGetMaxColumn (objDoc sheet / result typeVal lstdef sheetName r initSheet)
  70. (setq result 0)
  71.     (foreach lstdef objDoc
  72.         (setq typeVal (car lstdef))
  73.         (if (= (substr typeVal 1 5) "cell=")
  74.             (progn
  75.                 (setq sheetName (substr typeVal (+ 2 (vl-string-search "\t" typeVal))))
  76.                 (if (= sheet sheetName)
  77.                     (progn
  78.                         (setq initSheet (vl-string-search ":" typeVal))
  79.                         (setq r (substr typeVal (+ 2 initSheet) (- (vl-string-search "\t" typeVal) (1+ initSheet))))
  80.                         (if (> (ascii r) result)
  81.                             (setq result (ascii r))
  82.                         )
  83.                     )
  84.                 )
  85.             )
  86.         )
  87.     )
  88. result
  89. )

  90. ;funzione che ritorna il numero di riga pi?alto usato per un dato Sheet
  91. (defun FODSGetMaxRow (objDoc sheet / result typeVal lstdef sheetName r)
  92. (setq result 0)
  93.     (foreach lstdef objDoc
  94.         (setq typeVal (car lstdef))
  95.         (if (= (substr typeVal 1 5) "cell=")
  96.             (progn
  97.                 (setq sheetName (substr typeVal (+ 2 (vl-string-search "\t" typeVal))))
  98.                 (if (= sheet sheetName)
  99.                     (progn
  100.                         (setq r (substr typeVal 6 (- (vl-string-search ":" typeVal) 5)))
  101.                         (if (> (atoi r) result)
  102.                             (setq result (atoi r))
  103.                         )
  104.                     )
  105.                 )
  106.             )
  107.         )
  108.     )
  109. result
  110. )

  111. ;ritorna l'elenco dei fogli definiti返回列表的表定义
  112. (defun FODSGetSheets (objDoc / result lstdef typeVal tp sheetName)
  113.     (foreach lstdef objDoc
  114.         (setq typeVal (car lstdef))
  115.         (if (= (substr typeVal 1 5) "cell=")
  116.             (progn
  117.                 (setq sheetName (substr typeVal (+ 2 (vl-string-search "\t" typeVal))))
  118.                 (if (not (member sheetName result))
  119.                     (setq result (cons sheetName result))
  120.                 )
  121.             )
  122.         )
  123.     )
  124.     (reverse result)
  125. )

  126. ;crea un oggetto/lista per i file FODS创建一个对象文件fods /列表
  127. (defun FODSNew ( / result)
  128.     (list '("generator" "FODS Libryary"))
  129. )

  130. ;aggiunge una cella a un oggetto/list FODS添加一个列表对象的一个单元/ fods
  131. (defun FODSAddCell (objDoc sheetName row column value / typeVal lstVal search)
  132.     (if (= (type row) 'INT)
  133.         (setq row (itoa row))
  134.     )
  135.     (setq column (strcase column))
  136.     (setq typeVal (strcat "cell=" row ":" column "\t" sheetName))
  137.    
  138.     (setq search (assoc typeVal objDoc))
  139.     (setq lstVal (list typeVal value))
  140.     (if search
  141.         (progn ;sostituzione vecchio valore
  142.             (subst lstVal search objDoc)
  143.         )
  144.         (progn ;nuovo valore
  145.             (reverse (cons lstVal (reverse objDoc)))
  146.         )
  147.     )
  148. )

  149. ;scrive un file FODS写一个文件fods
  150. (defun FODSWrite (objDoc fileName / sheetsList sheet maxCol maxRow lstVal result c r value idf)
  151.     ;TODO : apertura file
  152.     (setq idf (open fileName "w"))
  153.     (if idf
  154.         (progn   
  155.             (write-line FODS-FILE-HEADER idf)
  156.             
  157.             (write-line "<office:meta>" idf)
  158.             (write-line (strcat "<meta:generator>"
  159.                             (cadr (assoc "generator" objDoc))
  160.                                 "</meta:generator>") idf)
  161.             (write-line "</office:meta>" idf)                     
  162.             (write-line "<office:body>" idf)
  163.             (write-line "<office:spreadsheet>" idf)
  164.             (setq sheetsList (FODSGetSheets objDoc))
  165.             (foreach sheet sheetsList
  166.                 ;inizio Sheet
  167.                 (write-line (strcat "<table:table table:name=\"" sheet "\">") idf)
  168.                
  169.                 (setq maxCol (FODSGetMaxColumn objDoc sheet))
  170.                 (setq maxRow (FODSGetMaxRow objDoc sheet))        
  171.                 (setq r 1)
  172.                 ;numero colonne
  173.                 (write-line (strcat "<table:table-column table:number-columns-repeated=\""
  174.                                     (itoa (- maxCol 64))
  175.                                     "\"/>"
  176.                             )
  177.                         idf)
  178.                 (while (<= r maxRow)
  179.                     ;inizio riga
  180.                     (write-line "<table:table-row>" idf)
  181.                     (setq c 65) ;A
  182.                     (while (<= c maxCol)
  183.                         (setq value (FODSGetCellValue objDoc sheet r (chr c)))
  184.                         ;inserimento linea vuota o piena
  185.                         (if value
  186.                             (progn
  187.                                 (setq value (vl-string-subst "<" "<" value))
  188.                                 (setq value (vl-string-subst ">" ">" value))
  189.                                 (write-line "<table:table-cell office:value-type=\"string\" calcext:value-type=\"string\">" idf)
  190.                                 (write-line (strcat "<text:p>" value "</text:p>") idf)
  191.                                 (write-line "</table:table-cell>" idf)                              
  192.                             )
  193.                             (progn
  194.                                 (write-line "<table:table-cell/>" idf)
  195.                             )
  196.                         )
  197.                         (setq c (1+ c))
  198.                     )            
  199.                     (setq r (1+ r))
  200.                     ;fine riga
  201.                     (write-line "</table:table-row>" idf)
  202.                 )
  203.                 ;fine Sheet
  204.                 (write-line "</table:table>" idf)
  205.             )
  206.             (write-line "</office:spreadsheet>" idf)
  207.             (write-line "</office:body>" idf)
  208.             (write-line "</office:document>" idf)
  209.             
  210.             (close idf)
  211.         );endp
  212.     );Endif
  213.     result
  214. )

  215. ;funzione di test
  216. (defun FODSTest ( / )
  217.    
  218.     (setq objDoc (FODSNew))
  219.     ;(print objDoc)
  220.     (setq objDoc (FODSAddCell objDoc "Sheet1" "1" "A" "cioa ciao"))
  221.     (setq objDoc (FODSAddCell objDoc "Sheet1" "2" "A" "uella <A>"))
  222.     (setq objDoc (FODSAddCell objDoc "Sheet1" "2" "c" "uella <2c>"))
  223.     (setq objDoc (FODSAddCell objDoc "Sheet1" 3 "b" "essere o non essere1"))
  224.     (setq objDoc (FODSAddCell objDoc "Sheet1" 2 "a" "sostituito"))
  225.     (setq objDoc (FODSAddCell objDoc "Sheet1" 15 "b" "essere o non essere2"))
  226.     (setq objDoc (FODSAddCell objDoc "Sheet2" 3 "b" "essere o non essere3"))
  227.     (setq objDoc (FODSAddCell objDoc "Sheet2" 2 "a" "il mio valore"))
  228.     (setq objDoc (FODSAddCell objDoc "Sheet3" 7 "a" "valore X"))
  229.     (setq objDoc (FODSAddCell objDoc "Sheet3" 3 "f" "valore X2"))

  230.     ;(print objDoc)
  231.     ;(print (FODSGetSheets objDoc))
  232.     ;(print (FODSGetMaxRow objDoc "Sheet3"))
  233.     ;(print (chr (FODSGetMaxColumn objDoc "Sheet3")))
  234.     ;(print (FODSGetCellValue objDoc "Sheet1" 2 "A"))
  235.    
  236.     (FODSWrite objDoc "c:\\temp\\test2.fods")
  237.     (alert "procedura termianta")
  238. (prin1)
  239. )


  240. ;(FODSTest)

"觉得好,就打赏"
还没有人打赏,支持一下
发表于 2016-3-23 12:57 | 显示全部楼层
有啥功能?
 楼主| 发表于 2016-3-23 13:00 | 显示全部楼层
yuanziyou 发表于 2016-3-23 12:57
有啥功能?

直接创建fods文件,支持libreoffice的
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-4-26 16:12 , Processed in 0.733356 second(s), 29 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表