簡體   English   中英

使用apache ant命令將值存儲到Excel單元格

[英]Using apache ant commands to store value to excel cell

我很好奇是否可以將值存儲到excel電子表格單元格中嗎? 如果是這樣,如何去完成這一工作? 我也有多個值,我想將它們存儲到相同的Excel工作表中,但存儲在不同的單元格中(例如A2或B1)。

例如,假設我有一個要粘貼到單元格A1中的值,現在,我實際上可以使用以下命令:

<echo append="true" file="file.xls" message="1" /> 

這將在單元格A1中存儲“ 1”,如果我再次運行同一命令,它將在單元格A1中也存儲“ 1”,緊鄰原始回顯。 但是我想要在另一個單元格中添加另一個值。

我查看了有關此主題的其他stackoverflow帖子並搜索了google,但找不到確切案例的答案。 如果您有更好的主意,請告訴我,謝謝。

這是我使用的鏈接:

屬性文件

其他stackoverflow帖子

Excel不是要解析和編寫的簡單文件格式。

下面的示例演示如何創建一個寫入excel文件的宏:

<excelWrite file="target/workbook.xlsx" values="Hello,world"/>

該宏使用Apache POI Java庫。

運行構建將生成一個excel文件

├── build.xml
└── target
    └── workbook.xlsx

補充筆記:

  • Apache ivy自動安裝並用於管理第三方jar依賴項
  • 使用嵌入式Groovy腳本避免了編寫和編譯ant任務的需要。

build.xml

<project name="demo" default="build" xmlns:ivy="antlib:org.apache.ivy.ant">

   <!--
   ==========
   Properties
   ==========
   -->
   <property name="build.dir" location="target"/>

   <available classname="org.apache.ivy.Main" property="ivy.installed"/> 

   <!--
   ======
   Macros
   ======
   -->
   <macrodef name="excelWrite">
      <attribute name="file"/>
      <attribute name="values"/>
      <attribute name="sheetName" default="ANT demo"/>
      <sequential>
         <ivy:cachepath pathid="build.path">
            <dependency org="org.codehaus.groovy" name="groovy-all" rev="2.2.2" conf="default"/>
            <dependency org="org.apache.poi" name="poi-ooxml" rev="3.10-FINAL" conf="default"/>
         </ivy:cachepath>

         <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>

         <groovy>
         import org.apache.poi.ss.usermodel.Workbook
         import org.apache.poi.xssf.usermodel.XSSFWorkbook
         import org.apache.poi.ss.usermodel.CreationHelper
         import org.apache.poi.ss.usermodel.Sheet
         import org.apache.poi.ss.usermodel.Row

         Workbook wb = new XSSFWorkbook();
         Sheet sheet = wb.createSheet("@{sheetName}");
         CreationHelper helper = wb.getCreationHelper();

         // Write data to a single row
         short rowpos = 0;
         short colpos = 0;
         Row row = sheet.createRow(rowpos);

         "@{values}".split(",").each {
            row.createCell(colpos++).setCellValue(helper.createRichTextString(it));
         }

         // Ensure parent directory exists
         def file = new File("@{file}")
         file.getParentFile().mkdirs()

         project.log "Writing Excel file: "+file
         file.withOutputStream {
             wb.write(it)
         }
         </groovy>
      </sequential>
   </macrodef>

   <!--
   =============
   Project setup
   =============
   -->
   <target name="install-ivy" description="Install ivy" unless="ivy.installed">
      <mkdir dir="${user.home}/.ant/lib"/>
      <get dest="${user.home}/.ant/lib/ivy.jar" src="http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.3.0/ivy-2.3.0.jar"/>

      <fail message="Ivy has been installed. Run the build again"/>
   </target>

   <!--
   ==========
   Main logic
   ==========
   -->
   <target name="build" depends="install-ivy" description="Create an Excel file">
      <excelWrite file="${build.dir}/workbook.xlsx" values="Hello,world"/>
   </target>

   <!--
   ===============
   Project Cleanup
   ===============
   -->
   <target name="clean" description="Cleanup build files">
      <delete dir="${build.dir}"/>
   </target>

   <target name="clean-all" depends="clean" description="Additionally purge ivy cache">
      <ivy:cleancache/>
   </target>

</project>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM