简体   繁体   English

使用apache ant命令将值存储到Excel单元格

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

I was curious if it was possible to store values into excel spreadsheet cells? 我很好奇是否可以将值存储到excel电子表格单元格中吗? 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). 我也有多个值,我想将它们存储到相同的Excel工作表中,但存储在不同的单元格中(例如A2或B1)。

For example, say that I have a value that I want to stick into cell A1, right now, I can actually using this command: 例如,假设我有一个要粘贴到单元格A1中的值,现在,我实际上可以使用以下命令:

<echo append="true" file="file.xls" message="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. 这将在单元格A1中存储“ 1”,如果我再次运行同一命令,它将在单元格A1中也存储“ 1”,紧邻原始回显。 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. 我查看了有关此主题的其他stackoverflow帖子并搜索了google,但找不到确切案例的答案。 Please let me know if you have any better ideas, thanks. 如果您有更好的主意,请告诉我,谢谢。

Here are the links that I used: 这是我使用的链接:

propertyfile 属性文件

other stackoverflow post 其他stackoverflow帖子

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: 下面的示例演示如何创建一个写入excel文件的宏:

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

The macro uses the Apache POI java library. 该宏使用Apache POI Java库。

Example

Running the build will generate an excel file 运行构建将生成一个excel文件

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

Additional notes: 补充笔记:

  • Apache ivy is automatically installed and used to manage 3rd party jar dependencies Apache ivy自动安装并用于管理第三方jar依赖项
  • Using an embedded groovy script avoids the need to write and compile an ant task. 使用嵌入式Groovy脚本避免了编写和编译ant任务的需要。

build.xml 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.

相关问题 使用Apache POI获取excel单元格显示值 - fetch excel cell display value using Apache POI 使用Apache POI保留excel单元格值的单引号前缀 - Preserve Single Quote Prefix of Cell Value of excel using Apache POI 使用Apache POI从Excel格式化HTML格式化单元格值 - HTML Formatted Cell value from Excel using Apache POI 如何使用Apache POI从excel工作表的单元格中读取外来字符,并使用SQLITE将它们存储在数据库中? - How to read foreign characters from a cell in excel sheet using Apache POI and store them in database using SQLITE? Apache POI读取Excel单元格的公式值 - Apache POI reading formula value of excel cell 使用Apache POI从Excel读取单元格 - Reading a cell from Excel using Apache POI 如何使用apache poi检查excel中的单元格值是否包含alt+enter? - How to check if a cell value in excel contains alt+enter using apache poi? 如何正确地将这个“奇怪的”单元格值转换为 Date 对象,使用 Apache POI 检索解析 Excel? - How to correctly convert into a Date object this "strange" cell value retrieve parsing an Excel using Apache POI? 使用Apache POI Java从Excel获取具有日期公式的单元格的Date值 - Get Date value of cell with date formula from Excel using Apache POI Java 如何使用 apache poi(不带格式)从 excel 获取日期单元格值 - how to get date cell value as it is from excel using apache poi (without formatting)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM