【问题标题】:Using apache ant commands to store value to excel cell使用 apache ant 命令将值存储到 excel 单元格
【发布时间】:2014-03-29 04:55:30
【问题描述】:

我很好奇是否可以将值存储到 Excel 电子表格单元格中?如果是这样,人们将如何完成这项工作?我还有多个值,我想将它们存储到同一个 Excel 工作表中,但存储在不同的单元格中(如 A2 或 B1)。

例如,假设我有一个值要粘贴到单元格 A1 中,现在,我实际上可以使用以下命令:

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

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

我查看了有关此主题的其他 stackoverflow 帖子并搜索了谷歌,但我找不到我的确切情况的答案。如果您有更好的想法,请告诉我,谢谢。

以下是我使用的链接:

propertyfile

other stackoverflow post

【问题讨论】:

  • 我想你会发现你的echo 不起作用。 xls 文件格式复杂,您必须使用 Excel 提供的 API 来操作它们。
  • 我不知道您的问题是什么......您要求使用 ANT 解决方案在 Excel 中存储数据。
  • 我正在努力实施您的答案,您可能正在做某事。无论如何,我只是想找到一个将数据存储到 excel 单元格中的 ant 命令。
  • 我只需要知道...你为什么要这样做?

标签: excel ant webtest


【解决方案1】:

Excel 不是一种易于解析和写入的文件格式。

以下示例演示如何创建写入 excel 文件的宏:

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

宏使用Apache POI java 库。

示例

运行构建会生成一个excel文件

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

补充说明:

  • Apache ivy 自动安装并用于管理 3rd 方 jar 依赖项
  • 使用嵌入式 groovy script 避免了编写和编译 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-30
    • 1970-01-01
    • 2013-05-12
    • 2013-08-08
    • 2016-02-18
    相关资源
    最近更新 更多