使用apache ant命令将值存储到excel单元格 [英] Using apache ant commands to store value to excel cell

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

问题描述

如果可以将值存储到Excel表单单元格中,我很好奇如果是,如何完成这个?我也有多个值,我想存储到同一个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" /> 

这将在单元格A1中存储1,如果我再次运行相同的命令,它将存储在单元格A1中也是1,就在原始的回声旁边。但我想要的是将另一个值添加到不同的单元格中。

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.

我已经看过关于这个主题的其他stackoverflow文章,并搜索了google,但是我找不到我的确切情况的答案。如果你有更好的想法,谢谢。

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.

以下是我使用的链接:

属性文件

其他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 自动安装并用于管理第三方jar依赖性

  • 使用嵌入式 groovy脚本避免了写入的需要并编译一个蚂蚁任务。

  • 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天全站免登陆