[英]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: 这是我使用的链接:
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库。
Running the build will generate an excel file 运行构建将生成一个excel文件
├── build.xml
└── target
└── workbook.xlsx
Additional notes: 补充笔记:
<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.