Convert Excel VBA to C++Builder OLE API - vba

how to convert the following VBA code to c++ builder in OLE? Thanks.
Range("A10:B28").Select
ActiveSheet.Shapes.AddChart2(240, xlXYScatter).Select
ActiveChart.SetSourceData Source:=Range("Sheet1!$A$10:$B$28")
ActiveSheet.Shapes("test1").IncrementLeft -288.5293700787
ActiveSheet.Shapes("test1").IncrementTop -39.7059055118
I have tried the following code, the c++ builder crash at last line
#include <excel_2k.h>
outXL = Variant::CreateObject("excel.application");
outXL.OlePropertySet("Visible", true);
outWorkbooks = outXL.OlePropertyGet("Workbooks");
outWorkbook = outWorkbooks.OleFunction("Open", "D:\\test.xls");
outWorkSheets = outWorkbook.OlePropertyGet("Worksheets");
outActiveSheet = outWorkbook.OlePropertyGet("Worksheets", 0);
outActiveSheet.OlePropertyGet("Activate");
Range=outActiveSheet.OlePropertyGet("Range",
outActiveSheet.OlePropertyGet("Cells",5,1),
outActiveSheet.OlePropertyGet("Cells",20,2));
Chart = outActiveSheet.OlePropertyGet("Shapes").OleFunction("AddChart2");
//=========This line Dump ERROR=========
Chart.OlePropertySet("ChartType",xlXYScatter);

IDK, if this is the answer, but ISTM you need to modify the code here
Chart = outActiveSheet.OlePropertyGet("Shapes").OleFunction("AddChart2", 240, xlXYScatter);
That is the way it is called in the VBA. You just have OleFunction("AddChart2")
What is the value of Chart after the function call?

I have fixed this issue, please reference the following code...
Chart = outXL.OlePropertyGet("Charts").OleFunction("Add");
Chart.OlePropertySet("ChartType", xlXYScatter);
Range=outActiveSheet.OlePropertyGet("Range",
outActiveSheet.OlePropertyGet("Cells",iDrawRowFrom,iDrawColFrom),
outActiveSheet.OlePropertyGet("Cells",iDrawRowTo,iDrawColTo));
Chart.OleProcedure("SetSourceData", Range);
Chart.OleFunction("Location", 2, strOutSheetName.c_str());

Related

Janus GridEX - C# How to show multi-line header using columnsets at runtime?

I have already tried:
How to wrap header caption in Janus GridEx
and
https://www.c-sharpcorner.com/forums/table-column-set
and looked through Janus documentation at http://codelibraries.blogspot.com/search/label/Janus%20GridEX.
I have a business requirement to show wrapped column header. For example, if column header is:
This is column header
need to show
This is column
header
As it is right now (before I tried implementing code for this requirement), code looked like:
dgMulti.SetDataBinding(bulkTable, "")
dgMulti.RetrieveStructure()
Dim column As Janus.Windows.GridEX.GridEXColumn
For Each column In dgMulti.RootTable.Columns
column.Width = 150
Next
where bulkTable is a System.Data.DataTable object.
After visiting above links, I tried suggestions and used ColumnSets as shown below and added after above code:
Dim CSET As New Janus.Windows.GridEX.GridEXColumnSet()
CSET.Caption = "YourCaption"
CSET.HeaderAlignment = Janus.Windows.GridEX.TextAlignment.Center
CSET.ColumnCount = 2
CSET.Width = 150
CSET.Key = "YourCaptionKey"
' FOLLOWING LINE THROW ERROR
**CSET.Add(New Janus.Windows.GridEX.GridEXColumn(dgMulti.RootTable.Columns(0).Key, dgMulti.RootTable.Columns(0).ColumnType), 0, 1)
CSET.Add(New Janus.Windows.GridEX.GridEXColumn(dgMulti.RootTable.Columns(1).Key, dgMulti.RootTable.Columns(1).ColumnType), 0, 2)**
dgMulti.RootTable.ColumnSets.Add(CSET)
dgMulti.RootTable.ColumnSetHeaderLines = 2
dgMulti.RootTable.CellLayoutMode = Janus.Windows.GridEX.CellLayoutMode.UseColumnSets
Error: Operation is not valid due to the current state of the object.
Can you please help what I am missing? I believe I need to somehow link the columns with ColumnSets but how?
You don't need columnsets for this.
I can't get a deeplink to the janus forums thread with this answer, but here is the copy/pasted answer:
You can put a CR Character if you can determine where to wrap.
in e.g.
Column.Caption = Now.ToString("ddd" & vbCr & "d MMM");
You can see the janus support forums here: https://www.janusys.com. Click through to the Winforms GridEX forum, and use search phrase "wrap".
The website is basically unusable in Firefox, I fallback to Chrome to browse.
Dim CSET As New Janus.Windows.GridEX.GridEXColumnSet()
CSET.Caption = "YourCaption"
CSET.HeaderAlignment = Janus.Windows.GridEX.TextAlignment.Center
CSET.ColumnCount = 2
CSET.Width = 150
CSET.Key = "YourCaptionKey"
dgMulti.RootTable.ColumnSets.Add(CSET)
CSET.Add(dgMulti.RootTable.Columns.Add("Yourcolumnkey"), 0, 0)
CSET.Add(dgMulti.RootTable.Columns.Add("Yourcolumnkey"), 0, 1)
dgMulti.RootTable.ColumnSetHeaderLines = 2
dgMulti.RootTable.CellLayoutMode = Janus.Windows.GridEX.CellLayoutMode.UseColumnSets

Ironpython in Spotfire - draw curve and modify chart

I have a code that changes my visualisation type from a Line to a Bar chart based on a property type. I need add a query to draw a straight line based on a property within my visualisation. The code I have so far is this:
from Spotfire.Dxp.Application.Visuals import VisualContent
from Spotfire.Dxp.Application.Visuals import VisualTypeIdentifiers
vc1 = viz1.As[VisualContent]()
Yaxis=vc1.YAxis.Expression
Xaxis=vc1.XAxis.Expression
ColorAxis=vc1.ColorAxis.Expression
if type=="LINE":
viz1.TypeId = VisualTypeIdentifiers.LineChart
if type == "BAR":
viz1.TypeId = VisualTypeIdentifiers.BarChart
vc1.XAxis.Expression=Xaxis
vc1.YAxis.Expression=Yaxis
vc1.ColorAxis.Expression=ColorAxis
Thanks in advance for your help!
from Spotfire.Dxp.Application.Visuals import LineChart
if vis.As[LineChart]().FittingModels[0].Enabled == False:
vis.As[LineChart]().FittingModels[0].Enabled = True
else:
vis.As[LineChart]().FittingModels[0].Enabled = False
This code requires a vis parameter defined as Type: Visualization Value: Page > Line Chart

Set objMapWindow = gobjGeoApp.ActiveWindow

above following command is written in VB, where objMapWindow is object,how to write same command in C#.
try these
dynamic objMapWindow = gobjGeoApp.ActiveWindow;
object objMapWindow = gobjGeoApp.ActiveWindow;

vb - I am trying to print other form but this happens:

(this is my first post so correct me if I'm doing something wrong.)
Here is a bit of my code:
imp.id.Text = globalvars.mdataset.Tables("table1").Rows(0).Item("id")
imp.date.Text = globalvars.mdataset.Tables("table1").Rows(0).Item("date")
imp.hour.Text = globalvars.mdataset.Tables("table1").Rows(0).Item("hour").ToString
imp.PrintForm1.PrinterSettings.DefaultPageSettings.Margins = New Printing.Margins(10, 10, 10, 10)
imp.Show()
imp.Focus()
imp.PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview
imp.PrintForm1.Print()
and what happens is that when it gives me the preview the page is empty... but the weird thing is that when i place a msgbox just before the printing action, the preview goes right and shows me what i want to print....
any tips?
EDIT: imp is the form where the printform is
Try to add this ..
....
Application.DoEvents
imp.PrintForm1.Print()

How to get chart info from an Excel spreadsheet using Apache POI?

Is it possible to extract chart information from an Office 2007 (xlsx / OpenXML) spreadsheet using Apache POI? I've managed to read in the spreadsheet and even get the part that refers to the chart but not sure how I can retrieve any info from this part e.g. Type of chart, chart data etc.
XSSFWorkbook xwb = new XSSFWorkbook("charts_lines.xlsx");
XSSFSheet sheet = xwb.getSheetAt(0);
I can also iterate through the package parts to retrieve the chart part, but I don't see how I then go on to retrieve any info about the chart?
Note, I'm not interested in creating charts using POI, just read as much chart info as is possible to do...I'm also not saving an xlsx. I simply wish to extract line colours, labels, data, chart type (pie, line, bar etc.)
There isn't a high level representation at the moment, so you'll need to drop down into the xmlbeans level and work with the low level CT* objects.
For Chart Sheets, there's XSSFChartSheet which will give you a CTChartsheet object, which has a little bit of info.
For both XSSFChart and XSSFChartSheet (regular and chart sheets), you'll need to go via the drawings to get the charts. Each sheet with charts on it should have one Drawing, and the charts get linked from the drawing, rather than the sheet itself.
As of r1090442 (so POI 3.8 or newer), there's a method on XSSFDrawing to give you all the XSSFChart objects (which are wrappers around the /charts/chart#.xml part). If you're on a really really old version of POI, use the CTDrawing to get the details of the chart, grab the /charts/chart#.xml part that corresponts, and then have xmlbeans give you the CT objects for it. Either way that'll let you get the titles, types, data ranges etc.
It is a bit fiddly though, so do please consider sending in a patch to POI if you get something good worked out for working with the CTChart objects!
you can read chart data as XML using XSSFDrawing
like
XSSFDrawing drawing = ((XSSFSheet)sheet).createDrawingPatriarch();
System.out.println(drawing.getCTDrawing().toString());
will print whole chart as XMl and also using
drawing.getCharts();
you can add Iterator to it to browse chart
I don't know the exact answer to your question, but the OpenXML SDK 2.0 comes with a DocumentReflector.exe tool that will show you exactly how the chart is defined (including all relationships between the SpreadsheetML and the DrawingML packages). There is some more info on this tool in this article.
Yes, It is possible to read any type of chart using Apache POI. But Before reading any chart information you need to know what XML string you are receiving because this could be different based on the chart type i.e. pie, line, bar, scatter or mixed (a combination of two or more) charts, etc. Therefore you approach will be different for different type of chart.
For a simple bar chart like this:
Your XML will look something like this:
<xml-fragment ...>
<c:title>
<c:tx>
<c:rich>
...
<a:p>
...
<a:r>
...
<a:t>Employee Salary</a:t>
</a:r>
</a:p>
</c:rich>
</c:tx>
...
</c:title>
...
<c:plotArea>
...
<c:barChart>
...
<c:ser>
...
<c:cat>
<c:strRef>
...
<c:strCache>
<c:ptCount val="5"/>
<c:pt idx="0">
<c:v>Tom</c:v>
</c:pt>
<c:pt idx="1">
<c:v>John</c:v>
</c:pt>
<c:pt idx="2">
<c:v>Harry</c:v>
</c:pt>
<c:pt idx="3">
<c:v>Sam</c:v>
</c:pt>
<c:pt idx="4">
<c:v>Richa</c:v>
</c:pt>
</c:strCache>
</c:strRef>
</c:cat>
<c:val>
<c:numRef>
...
<c:numCache>
<c:formatCode>"$"#,##0</c:formatCode>
<c:ptCount val="5"/>
<c:pt idx="0">
<c:v>1000</c:v>
</c:pt>
<c:pt idx="1">
<c:v>700</c:v>
</c:pt>
<c:pt idx="2">
<c:v>300</c:v>
</c:pt>
<c:pt idx="3">
<c:v>900</c:v>
</c:pt>
<c:pt idx="4">
<c:v>800</c:v>
</c:pt>
</c:numCache>
</c:numRef>
</c:val>
...
</c:ser>
...
</c:barChart>
...
</c:plotArea>
...
</xml-fragment>
Now based on the above XML String we can use CT* classes and its various methods to traverse through the whole XML using Apache POI. Let's see how to read Chart Title, Labels(Employee names) and Series(Employee salaries) using POI:
Workbook workbook = new XSSFWorkbook(new File(PATH));
Sheet sheet = workbook.getSheet("GraphSheet");
XSSFSheet xsheet = (XSSFSheet) sheet;
XSSFDrawing drawing = xsheet.getDrawingPatriarch();
if (drawing != null) {
List<XSSFChart> charts = drawing.getCharts();
for (int chartIndex = 0; charts != null && chartIndex < (charts.size()); chartIndex++) {
XSSFChart chart = charts.get(chartIndex);
CTChart chart2 = chart.getCTChart();
CTPlotArea plot = chart2.getPlotArea();
System.out.println("Chart Title :" + chart2.getTitle().getTx().getRich().getPArray(0).getRArray(0).getT());
CTBarSer[] ctScaSerList = plot.getBarChartArray(0).getSerArray();
for (CTBarSer ctLineSer : ctScaSerList) {
CTStrVal[] ctStrVals = ctLineSer.getCat().getStrRef().getStrCache().getPtArray();
for (int i = 0; i < ctStrVals.length; i++) {
System.out.print(ctStrVals[i].getV() + ",");
}
System.out.println();
CTNumVal[] ctXNumVal = ctLineSer.getVal().getNumRef().getNumCache().getPtArray();
for (int i = 0; i < ctXNumVal.length; i++) {
System.out.print(ctXNumVal[i].getV() + ",");
}
}
}
}
Console:
Chart Title :Employee Salary
Tom,John,Harry,Sam,Richa,
1000,700,300,900,800,
Note: Here, the idea is to first read the XML String(because could be different based on your graph type) and then traverse the whole XML accordingly.