我想在Groovy中创建excel文件,然后绘制它们。此代码取自使用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"
我如何修改它以在Groovy中工作?
答案 0 :(得分:13)
你需要groovy才能使用COM。这个page的底部是自动化Excel的一个例子。
<强> EDITS 强>
这是你的例子翻译成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
答案 1 :(得分:1)
马克上面的答案就是一个很好的例子。如果您进行一些简单的更改,事件就更容易理解:
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
答案 2 :(得分:0)
另一个选项可能为Apache POI,具体取决于您实际需要的所有内容。
对于初学者来说,任何一个选项都会很复杂;简单的方法就是使用shell。
答案 3 :(得分:0)
我也会去Apache POI。可以在busy developer guide找到一个工作示例。还有GSheets,一个用于Apache POI的瘦的groovy包装器,请参阅blog post和unit test以获取示例用法。
答案 4 :(得分:0)
可能最简单的(就外部代码而言)是文本格式,单元格由&#39; \ t&#39;分隔。和线&#34; \ r \ n&#34;。 小心具有感知字符串但具有num字符(或类似日期)的单元格,最好以单撇号为前缀
此类格式可以通过剪贴板粘贴或从文件菜单中打开。