繁体   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