使用Apache Ant命令来存储值Excel单元格 [英] Using apache ant commands to store value to excel cell

查看:212
本文介绍了使用Apache Ant命令来存储值Excel单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇,如果有可能来存储值到Excel US preadsheet细胞?如果是这样,怎么会一次过有关完成此?我也有多个值,我想存储到同一个Excel表,但在不同的细胞(如A2或B1)。

I was curious if it was possible to store values into excel spreadsheet cells? And if so, how would one go about completing this? I also have multiple values that I would like to store into the same excel sheet but in different cells (like A2 or B1).

举例来说,假设我有我要坚持到单元格A1,现在值,其实我可以使用这个命令:

For example, say that I have a value that I want to stick into cell A1, right now, I can actually using this command:

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

这将存储1,在单元格A1,如果我再次运行相同的命令,将存储1,在单元格A1为好,毗邻原回声。但我想要的是有一个公司在不同的细胞又增加了价值。

This will store "1" in cell A1 and if I ran the same command again, it would store "1" in cell A1 as well, just next to the original echo. But I want is to have another value that's added in a different cell.

我看过了相关的主题帖计算器并搜查谷歌,但我无法找到一个答案,我的确切情况。请让我知道,如果你有更好的想法,谢谢。

I've looked at other stackoverflow posts about this topic and searched google, but I couldn't find an answer to my exact case. Please let me know if you have any better ideas, thanks.

下面是我用的链接:

propertyfile

<一个href=\"http://stackoverflow.com/questions/262015/how-to-store-apache-ant-property-value-in-file\">other StackOverflow的帖子

推荐答案

Excel是不解析,写一个简单的文件格式。

Excel is not a trivial file format to parse and write.

下面的例子演示了如何创建写入Excel文件宏:

The following example demonstrates how to create a macro that writes an excel file:

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

宏使用的Apache POI 的Java库。

运行的版本将产生一个Excel文件

Running the build will generate an excel file

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

其他注意事项:

  • Apache ivy is automatically installed and used to manage 3rd party jar dependencies
  • Using an embedded groovy script avoids the need to write and compile an ant task.
<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>

这篇关于使用Apache Ant命令来存储值Excel单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆