简体   繁体   English

如何使用Groovy编写Excel文件脚本

[英]How to script Excel files with Groovy

I want to create excel files in Groovy, then have them plotted. 我想在Groovy中创建excel文件,然后绘制它们。 This code was taken from an example using Microsoft's Shell Scripting language: 此代码取自使用Microsoft的Shell Scripting语言的示例:

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Add()
Set objWorksheet = objWorkbook.Worksheets(1)

objWorksheet.Cells(1,1) = "Operating System"
objWorksheet.Cells(2,1) = "Windows Server 2003"
objWorksheet.Cells(3,1) = "Windows XP"
objWorksheet.Cells(5,1) = "Windows NT 4.0"
objWorksheet.Cells(6,1) = "Other"

objWorksheet.Cells(1,2) = "Number of Computers"
objWorksheet.Cells(2,2) = 145
objWorksheet.Cells(3,2) = 987
objWorksheet.Cells(4,2) = 611
objWorksheet.Cells(5,2) = 41
objWorksheet.Cells(6,2) = 56

Set objRange = objWorksheet.UsedRange
objRange.Select

Set colCharts = objExcel.Charts
colCharts.Add()

Set objChart = colCharts(1)
objChart.Activate

objChart.HasLegend = FALSE
objChart.ChartTitle.Text = "Operating System Use"

How would I modify this to work in Groovy? 我如何修改它以在Groovy中工作?

You need groovy to work with COM . 你需要groovy与COM合作 Towards the bottom of this page is an example of automating Excel. 本页底部是Excel自动化的示例。

EDITS EDITS

Here's your example translated into Groovy (I ran this under Groovy 1.8.2): 这是你的例子翻译成Groovy(我在Groovy 1.8.2下运行):

import org.codehaus.groovy.scriptom.*
import org.codehaus.groovy.scriptom.tlb.office.excel.XlChartType
import org.codehaus.groovy.scriptom.tlb.office.excel.XlRowCol
import org.codehaus.groovy.scriptom.tlb.office.excel.XlChartLocation

// create a xls instance
def xls = new ActiveXObject("Excel.Application")

xls.Visible = true

Thread.sleep(1000)

// get the workbooks object
def workbooks = xls.Workbooks
// add a new workbook
def workbook  = workbooks.Add()

// select the active sheet
def sheet = workbook.ActiveSheet

cell = sheet.Range("A1")
cell.Value = "Operating System"
cell = sheet.Range("A2")
cell.Value = "Windows Server 2003"
cell = sheet.Range("A3")
cell.Value = "Windows XP"
cell = sheet.Range("A4")
cell.Value = "Windows NT 4.0"
cell = sheet.Range("A5")
cell.Value = "Other"

cell = sheet.Range("B1")
cell.Value = "Number of Computers"
cell = sheet.Range("B2")
cell.Value = 145
cell = sheet.Range("B3")
cell.Value = 987
cell = sheet.Range("B4")
cell.Value = 611
cell = sheet.Range("B5")
cell.Value = 41

def chart = workbook.Charts.Add(Scriptom.MISSING, sheet)  // create chart object
chart.ChartType = XlChartType.xl3DArea // set type to pie
chart.SetSourceData(sheet.Range("A1:B5"), XlRowCol.xlColumns) // set source data
chart.Location(XlChartLocation.xlLocationAsNewSheet) // add chart as new sheet

Mark's answer above was a great example. Mark上面的回答就是一个很好的例子。 It's event easier to follow if you make a couple simple changes: 如果您进行一些简单的更改,事件就更容易理解:

import org.codehaus.groovy.scriptom.*
import org.codehaus.groovy.scriptom.tlb.office.excel.*

def xls = new ActiveXObject("Excel.Application")

xls.Visible = true

Thread.sleep(1000)

// add a new workbook
def workbook  = xls.Workbooks.Add()

// select the active sheet
def sheet = workbook.ActiveSheet    

sheet.Range("A1").Value = "Operating System"
sheet.Range("A2").Value = "Windows Server 2003"
sheet.Range("A3").Value = "Windows XP"
sheet.Range("A4").Value = "Windows NT 4.0"
sheet.Range("A5").Value = "Other"

sheet.Range("B1").Value = "Number of Computers"
sheet.Range("B2").Value = 145
sheet.Range("B3").Value = 987
sheet.Range("B4").Value = 611
sheet.Range("B5").Value = 41

def chart = workbook.Charts.Add(Scriptom.MISSING, sheet)  // create chart object
chart.ChartType = XlChartType.xl3DArea // set type to pie
chart.SetSourceData(sheet.Range("A1:B5"), XlRowCol.xlColumns) // set source data
chart.Location(XlChartLocation.xlLocationAsNewSheet) // add chart as new sheet

I would also go for Apache POI . 我也会选择Apache POI A working example can be found at the busy developer guide . 可以在繁忙的开发人员指南中找到一个工作示例。 There is also GSheets , a thin groovy wrapper for Apache POI, see the blog post and the unit test for an example usage. 还有GSheets ,一个用于Apache POI的瘦的groovy包装器,请参阅博客文章单元测试以获取示例用法。

Probably simplest (in terms of external code) is text format, cells are delimited by '\\t' and lines "\\r\\n". 可能最简单的(就外部代码而言)是文本格式,单元格由'\\ t'和行“\\ r \\ n”分隔。 Be careful on cell having sense String but with num characters (or Date-alike), best to prepend with single apostrophe 小心具有感知字符串但具有num字符(或类似日期)的单元格,最好以单撇号作为前缀

Such format can be pasted by clipboard or open from file menu. 这种格式可以通过剪贴板粘贴或从文件菜单打开。

Another option may be Apache POI , depending on what all you need to actually implement. 另一种选择可能Apache POI ,具体取决于您实际需要实现的所有内容。

Either option will be complicated for a beginner; 对于初学者来说,这两种选择都会很复杂; the easy path would be to use the shell. 简单的方法是使用shell。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM