Module: Asposecellsjava::Converter

Defined in:
lib/asposecellsjava/converter.rb

Instance Method Summary collapse

Instance Method Details

#chart_to_imageObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/asposecellsjava/converter.rb', line 40

def chart_to_image()
    # Create a new Workbook.
    workbook = Rjb::import('com.aspose.cells.Workbook').new

    # Get the first worksheet.
    sheet = workbook.getWorksheets().get(0)

    # Set the name of worksheet
    sheet.setName("Data")

    # Get the cells collection in the sheet.
    cells = workbook.getWorksheets().get(0).getCells()

    # Put some values into a cells of the Data sheet.
    cells.get("A1").setValue("Region")
    cells.get("A2").setValue("France")
    cells.get("A3").setValue("Germany")
    cells.get("A4").setValue("England")
    cells.get("A5").setValue("Sweden")
    cells.get("A6").setValue("Italy")
    cells.get("A7").setValue("Spain")
    cells.get("A8").setValue("Portugal")
    cells.get("B1").setValue("Sale")
    cells.get("B2").setValue(70000)
    cells.get("B3").setValue(55000)
    cells.get("B4").setValue(30000)
    cells.get("B5").setValue(40000)
    cells.get("B6").setValue(35000)
    cells.get("B7").setValue(32000)
    cells.get("B8").setValue(10000)

    # Create chart
    chart_type = Rjb::import('com.aspose.cells.ChartType')
    chart_index = sheet.getCharts().add(chart_type.COLUMN, 12, 1, 33, 12)
    chart = sheet.getCharts().get(chart_index)

    # Set properties of chart title
    chart.getTitle().setText("Sales By Region")
    chart.getTitle().getFont().setBold(true)
    chart.getTitle().getFont().setSize(12)

    # Set properties of nseries
    chart.getNSeries().add("Data!B2:B8", true)
    chart.getNSeries().setCategoryData("Data!A2:A8")

    # Set the fill colors for the series's data points (France - Portugal(7 points))
    chart_points = chart.getNSeries().get(0).getPoints()

    color = Rjb::import('com.aspose.cells.Color')

    point = chart_points.get(0)
    point.getArea().setForegroundColor(color.getCyan())

    point = chart_points.get(1)
    point.getArea().setForegroundColor(color.getBlue())

    point = chart_points.get(2)
    point.getArea().setForegroundColor(color.getYellow())

    point = chart_points.get(3)
    point.getArea().setForegroundColor(color.getRed())

    point = chart_points.get(4)
    point.getArea().setForegroundColor(color.getBlack())

    point = chart_points.get(5)
    point.getArea().setForegroundColor(color.getGreen())

    point = chart_points.get(6)
    point.getArea().setForegroundColor(color.getMaroon())

    # Set the legend invisible
    chart.setShowLegend(false)

    # Get the Chart image
    img_opts = Rjb::import('com.aspose.cells.ImageOrPrintOptions').new
    image_format = Rjb::import('com.aspose.cells.ImageFormat')
    img_opts.setImageFormat(image_format.getPng())

    # Save the chart image file.
    chart.toImage(@data_dir + "MyChartImage.png", img_opts)

    # Print message
    puts "Convert chart to image successfully."
end

#excel_to_pdf(workbook) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/asposecellsjava/converter.rb', line 31

def excel_to_pdf(workbook)
    save_format = Rjb::import('com.aspose.cells.SaveFormat')

    # Save the document in PDF format
    workbook.save(@data_dir + "MyPdfFile.pdf", save_format.PDF)

    puts "Pdf saved successfully."
end

#html_to_excelObject



198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/asposecellsjava/converter.rb', line 198

def html_to_excel()
    load_format = Rjb::import('com.aspose.cells.LoadFormat')
    # Create an instance of HTMLLoadOptions and initiate it with appropriate LoadFormat
    options = Rjb::import('com.aspose.cells.HTMLLoadOptions').new(load_format.HTML)
    
    # Load the Html file through file path while passing the instance of HTMLLoadOptions class
    workbook = Rjb::import('com.aspose.cells.Workbook').new(@data_dir + "index.html", options)
    
    save_format = Rjb::import('com.aspose.cells.SaveFormat')
    #Save the results to disc in Xlsx format
    workbook.save(@data_dir + "output.xlsx", save_format.XLSX)

    puts "XLSX saved successfully."
end

#initializeObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/asposecellsjava/converter.rb', line 3

def initialize()
    @data_dir = File.dirname(File.dirname(File.dirname(__FILE__))) + '/data/'
    
    # Instantiating a Workbook object by excel file path
    workbook = Rjb::import('com.aspose.cells.Workbook').new(@data_dir + 'Book1.xls')
    
    # Converting Excel to PDF
    excel_to_pdf(workbook)

    # Converting Chart to Image
    chart_to_image()

    # Converting Worksheet to Image
    worksheet_to_image(workbook)

    # Converting Worksheet to SVG
    worksheet_to_svg(workbook)

    # Converting Worksheet to MHTML
    worksheet_to_mhtml(workbook)

    # Converting Worksheet to HTML
    worksheet_to_html(workbook)

    # Converting HTML to Excel
    html_to_excel()
end

#worksheet_to_html(workbook) ⇒ Object



187
188
189
190
191
192
193
194
195
196
# File 'lib/asposecellsjava/converter.rb', line 187

def worksheet_to_html(workbook)
    save_format = Rjb::import('com.aspose.cells.SaveFormat')
    # Specify the HTML saving options
    save = Rjb::import('com.aspose.cells.HtmlSaveOptions').new(save_format.M_HTML)

    # Save the document
    workbook.save(@data_dir + "output.html", save)

    puts "HTML saved successfully."
end

#worksheet_to_image(workbook) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/asposecellsjava/converter.rb', line 126

def worksheet_to_image(workbook)
    #Create an object for ImageOptions
    img_options = Rjb::import('com.aspose.cells.ImageOrPrintOptions').new
    
    # Set the image type
    image_format = Rjb::import('com.aspose.cells.ImageFormat')
    img_options.setImageFormat(image_format.getPng())
    
    # Get the first worksheet.
    sheet = workbook.getWorksheets().get(0)

    # Create a SheetRender object for the target sheet
    sr = Rjb::import('com.aspose.cells.SheetRender').new(sheet, img_options)
    
    j = 0
    while j < sr.getPageCount()
        # Generate an image for the worksheet
        sr.toImage(j, @data_dir + "mysheetimg_#{j}.png")
        j +=1
    end

    puts "Image saved successfully."
end

#worksheet_to_mhtml(workbook) ⇒ Object



176
177
178
179
180
181
182
183
184
185
# File 'lib/asposecellsjava/converter.rb', line 176

def worksheet_to_mhtml(workbook)
    save_format = Rjb::import('com.aspose.cells.SaveFormat')
    # Specify the HTML saving options
    sv = Rjb::import('com.aspose.cells.HtmlSaveOptions').new(save_format.M_HTML)

    # Save the document
    workbook.save(@data_dir + "convert.mht", sv)

    puts "MHTML saved successfully."
end

#worksheet_to_svg(workbook) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/asposecellsjava/converter.rb', line 150

def worksheet_to_svg(workbook)
    # Convert each worksheet into svg format in a single page.
    img_options = Rjb::import('com.aspose.cells.ImageOrPrintOptions').new
    save_format = Rjb::import('com.aspose.cells.SaveFormat')
    img_options.setSaveFormat(save_format.SVG)
    img_options.setOnePagePerSheet(true)
    
    # Convert each worksheet into svg format
    sheet_count = workbook.getWorksheets().getCount()

    i=0
    while i < sheet_count
        sheet = workbook.getWorksheets().get(i)

        sr = Rjb::import('com.aspose.cells.SheetRender').new(sheet, img_options)

        k=0
        while sr.getPageCount()
            # Output the worksheet into Svg image format
            sr.toImage(k, @data_dir + sheet.getName() + "#{k}.svg")
        end
    end

    puts "SVG saved successfully."
end