Java如何输出Word报告

本文介绍了使用Java生成Word报告的思路,包括定义Word和Excel模板,通过Excel宏填充数据和图表,最后调整样式和输出。注意事项涉及Excel图表的设置和Word模板的布局,以及解决下载和定时任务的问题。
摘要由CSDN通过智能技术生成

目录

思路:

注意事项


很多企业每月或每季都会出销售报告或绩效报告等,通常都是以PPT文件的形式上报给公司领导或客户。

因PPT在java运用中有所局限(有精通此方面的同行可具体沟通)本人做过的项目则是输出Word报告。

思路:

1、定义Word报告模板,编辑好通用的内容,数据和图表位置插入文本框,然后在文本框中插入书签名(方便调整位置)

2、定义Excel图表模板,编辑宏调整图表样式

 Public Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As LongPtr) As Long
    Public Declare PtrSafe Function CloseClipboard Lib "user32" () As Long
    Public Declare PtrSafe Function EmptyClipboard Lib "user32" () As Long

    
Sub adjustLineChart()

    Worksheets("净值走势").Activate
    Sheets("净值走势").Select
    ActiveSheet.ChartObjects("图表 1").Activate
    xlCategoryAmount = Excel.Application.WorksheetFunction.CountA(Sheet2.range("A2:A65536"))
    xTickAmount = Int(xlCategoryAmount / 6)
    ' MsgBox (xTickAmount)
    If xTickAmount <> 0 Then
    ActiveChart.Axes(xlCategory).TickLabelSpacing = xTickAmount
    End If
    
    Set hs300Set = Worksheets("净值走势").range("B2:B65536")
    hs300_max = Application.WorksheetFunction.Max(hs300Set)
    hs300_min = Application.WorksheetFunction.Min(hs300Set)
    
    Set productSet = Worksheets("净值走势").range("C2:C65536")
    product_max = Application.WorksheetFunction.Max(productSet)
    product_min = Application.WorksheetFunction.Min(productSet)
    
    line_max = IIf(hs300_max > product_max, hs300_max, product_max)
    line_min = IIf(hs300_min < product_min, hs300_min, product_min)
    'MsgBox (line_max - line_min)
    tmpUnit = Int((line_max - line_min) * 100 / 4) / 100
    If tmpUnit <> 0 Then
    ActiveChart.Axes(xlValue).MajorUnit = tmpUnit
    End If
    
      '按日期升序排序
    'Dim rng As range
    'Dim maxRow
   ' maxRow = ActiveSheet.UsedRange.Cells(1, 1).Row + ActiveSheet.UsedRange.Rows.Count - 1
  '  'Set rng = range("A" & maxRow)
 '  Worksheets("净值走势").range("A1:C" & maxRow).Sort _
      '  Key1:=Worksheets("净值走势").range("A1"), _
     '   Order1:=xlAscending, _
      '  Header:=xlYes
        
End Sub


Sub adjustColumnChart()

    Worksheets("行业持仓").Activate
    Sheets("行业持仓").Select
    ActiveSheet.ChartObjects("图表 2").Activate
    ActiveChart.ChartTitle.Text = Worksheets("净值走势").range("C1").Text & "行业持仓"
     With ActiveChart.ChartTitle.Format.TextFrame2.TextRange.Font
            .BaselineOffset = 0
            .NameFarEast = "SourceHanSansCN-Normal"
    End With
    
    Set hs300Set = Worksheets("行业持仓").range("B2:AA2")
    hs300_max = Application.WorksheetFunction.Max(hs300Set)
    hs300_min = Application.WorksheetFunction.Min(hs300Set)
    
    Set productSet = Worksheets("行业持仓").range("B3:AA3")
    product_max = Application.WorksheetFunction.Max(productSet)
    product_min = Application.WorksheetFunction.Min(productSet)
    
    line_max = IIf(hs300_max > product_max, hs300_max, product_max)
    line_min = IIf(hs300_min < product_min, hs300_min, product_min)
    tmpUnit = Int((line_max - line_min) * 100 / 4) / 100
    If tmpUnit <> 0 Then
    ActiveChart.Axes(xlValue).MajorUnit = tmpUnit
    End If
End Sub

Sub adjustJDColumnChart()

    Worksheets("季度行业持仓").Activate
    Sheets("季度行业持仓").Select
    ActiveSheet.ChartObjects("图表 2").Activate
    ActiveChart.ChartTitle.Text = Worksheets("净值走势").range("C1").Text & "季度行业持仓"
    With ActiveChart.ChartTitle.Format.TextFrame2.TextRange.Font
            .BaselineOffset = 0
            .NameFarEast = "SourceHanSansCN-Normal"
    End With
    

    Set hs300Set = Worksheets("季度行业持仓").range("B2:AA2")
    hs300_max = Application.WorksheetFunction.Max(hs300Set)
    hs300_min = Application.WorksheetFunction.Min(hs300Set)
    
    Set productSet = Worksheets("季度行业持仓").range("B3:AA3")
    product_max = Application.WorksheetFunction.Max(productSet)
    product_min = Application.WorksheetFunction.Min(productSet)
    
    line_max = IIf(hs300_max > product_max, hs300_max, product_max)
    line_min = IIf(hs300_min < product_min, hs300_min, product_min)
    tmpUnit = Int((line_max - line_min) * 100 / 4) / 100
    If tmpUnit <> 0 Then
    ActiveChart.Axes(xlValue).MajorUnit = tmpUnit
    End If
End Sub

Sub adjustText()

   Dim ShTest1 As Worksheet
   Dim ShTest2 As Worksheet
   Dim ShTest3 As Worksheet
   Dim ShTest4 As Worksheet
   Dim ShTest5 As Worksheet
   Dim ShTest6 As Worksheet
   Dim ShTest7 As Worksheet
   Dim introw As Double
  
   
   Set ShTest1 = Sheets("业绩指标")
   Set ShTest2 = Sheets("净值走势")
   Set ShTest3 = Sheets("月度收益率")
   Set ShTest4 = Sheets("行业持仓")
   Set ShTest5 = Sheets("季度行业持仓")
   Set ShTest7 = Sheets("组合暴露度")
   

   
   '设置文本字体、大小、颜色

    ShTest1.range("A2:B2").Font.Color = RGB(168, 33, 39)
    ShTest1.range("A2:B2").Font.Name = "思源黑体 CN Medium"
    
    If Len(ShTest1.range("B2:B2").Text) >= 8 Then
     ShTest1.range("A2:B2").Font.Size = 24
    Else
     ShTest1.range("A2:B2").Font.Size = 31
    End If
  
    ShTest1.range("B1").Font.Color = RGB(168, 33, 39)
    ShTest1.range("B1").Font.Size = 21
    ShTest1.range("B1").Font.Name = "思源黑体 CN Medium"
    
    ShTest1.range("A3:B3").Font.Color = RGB(168, 33, 39)
    ShTest1.range("A3:B3").Font.Size = 14
    ShTest1.range("A3:B3").Font.Name = "思源黑体 CN Medium"
  
    
    ShTest1.range("A5:C5").Font.Color = RGB(168, 33, 39)
    ShTest1.range("A5:C5").Font.Size = 16
    ShTest1.ra
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云梦楼兰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值