Export to excel, Column header display error in ColdFusion - excel

I am trying to get account numbers by a query and displaying those account numbers as column header after exporting into excel.
The following code is working fine but displaying wrongly:
<cfset variables.setHeader = "">
<cfoutput query="myLoop">
<cfset variables.setHeader = variables.setHeader & ", C_#myLoop.acc#>
</cfoutput>
The above code is displaying column header (just one sample data I am showing here) but it is showing as 'C_000391'. Which is wrong! For display, 'C_' should not be with account number, it is the account number which should create the column header alone. However, as soon as I am taking off 'C_' from infront of #myLoop.acc# it is giving the following error:
The column name 000391 is invalid.
Please let me know what will be the best solution(s) to resolve this toughest problem on earth :)
Thanks

Seems you are trying a form a comma separated list , try below , no looping required.
< cfset variables.setHeader = ValueList(myLoop.acc) >

Related

Setting Number Format to Accounting in VBA

With ActiveSheet
.Range("T26:T31").NumberFormat = "_(£* #,##0.00_);_(£* (#,##0.00);_(£* " - "??_);_(#_)"
I am trying to set the format of a cell range to Accounting, I have looked at the number format (above) and tried setting it to that, but I get a Type Mismatch.
I also tried doing Debug.Print Application.ActiveCell.NumberFormatLocal to find out how excel reads it, copied that in and still with no luck.
Anyone got any ideas?
You need to double any quotation marks inside the format code, so:
.Range("T26:T31").NumberFormat = "_(£* #,##0.00_);_(£* (#,##0.00);_(£* "" - ""??_);_(#_)"

Openpyxl returns wrong hyperlink address after delete_rows()

Problem: I have a program that scrapes Twitter and returns the results in an excel file. Part of each entry is a column containing a hyperlink to the Tweet and image included in the Tweet if applicable. Entries and hyperlinks work fine except when I run the following code to remove duplicate posts:
#Remove duplicate posts.
values = []
i = 2
while i <= sheet.max_row:
if sheet.cell(row=i,column=3).value in values:
sheet.delete_rows(i,1)
else:
values.append(sheet.cell(row=i,column=3).value)
i+=1
After running the duplicate removal snippet the hyperlinks point to what I assume is the offset of deleted entries. Here is the code for creating a Twitter entry:
sheet.cell(row=row, column=8).hyperlink = "https://twitter.com/"+str(tweet.user.screen_name)+"/status/"+str(tweet.id)
sheet.cell(row=row, column=8).style = "Hyperlink"
Expected Results: Should be able to remove duplicate entries and keep the hyperlink pointed to the correct address.
The hyperlinks point to the correct addresses for whatever reason when I change the code to the this:
sheet.cell(row=row, column=8).value = "https://twitter.com/"+str(tweet.user.screen_name)+"/status/"+str(tweet.id)
sheet.cell(row=row, column=8).style = "Hyperlink"
Requires a rapid double click to work as a hyperlink in the excel sheet versus the one click when inserting using .hyperlink.
So fixed but not fixed.

How can we include the cell formula while export to excel from .rdlc

In my rdlc report have following columns
SlNo, Item, Uom, Qty, Rate, Amount
Here the Amount field is a formula (Rate*Qty)
The report is working fine, and when i export to excel also displaying the values are correctly.
But my problem is, after export to excel, when i change the Qty or Rate columns in excel file the Amount is not get changed automatically, because the formula is missing in the excel cell.
How can we include the formula in Amount column while export to excel from .rdlc?
I'm afraid that this required behaviour isn't really possible by just using the rdlc rendering.
In my search I stumbled upon this same link that QHarr posted: https://social.msdn.microsoft.com/Forums/en-US/3ddf11bf-e10f-4a3e-bd6a-d666eacb5ce4/report-viewer-export-ms-report-data-to-excel-with-formula?forum=vsreportcontrols
I haven't tried the project that they're suggesting but this might possibly be your best solution if it works. Unfortunately I do not have the time to test it myself, so if you test this please share your results.
I thought of the following workaround that seems to work most of the times, but isn't really that reliable because the formula sometimes gets displayed as full-text instead of being calculated. But I guess this could be solved by editing the excel file just after being exported, and changing the cell properties of this column containing the formula or just triggering the calculate.
Using the built-in-field Globals!RenderFormat.Name you can determine the render mode, this way you can display the result correctly when the report is being rendered to something different than Excel. When you export to Excel, you could change the value of the cell to the actual formula.
To form the formula it's self you'll need to figure this out on your own, but the RowNumber(Scope as String) function can be of use here to determine the row number of your cells.
Here is a possible example for the expression value of your amount column
=IIF(Globals!RenderFormat.Name LIKE "EXCEL*", "=E" & Cstr(RowNumber("DataSet1")+2) & "*F" & Cstr(RowNumber("DataSet1")+2) ,Fields!Rate.Value * Fields!Qty.Value )
Now considering that this formula sometimes gets displayed as full-text, and you'll probably have to edit the file post-rendering. If it's too complicated to determine which row/column the cell is on, you could also do this post-rendering. But I believe that the above expression should be easy enough to use to get your desired result without having to do much after rendering.
Update: The following code could be used to force the calculation of the formula (post rendering)
var fpath = #"C:\MyReport.xlsx";
using (var fs = File.Create(fpath))
{
var lr = new LocalReport();
//Initializing your reporter
lr.ReportEmbeddedResource = "MyReport.rdlc";
//Rendering to excel
var fbytes = lr.Render("Excel");
fs.Write(fbytes, 0, fbytes.Length);
}
var xlApp = new Microsoft.Office.Interop.Excel.Application() { Visible = false };
var wb = xlApp.Workbooks.Open(fpath);
var ws = wb.Worksheets[1];
var range = ws.UsedRange;
foreach (var cell in range.Cells)
{
var cellv = cell.Text as string;
if (!string.IsNullOrWhiteSpace(cellv) && cellv.StartsWith("="))
{
cell.Formula = cellv;
}
}
wb.Save();
wb.Close(0);
xlApp.Quit();

pentaho modify and print a parameter while exporting to excel

I use pentaho report designer. I have got businessdate parameter in my prpt file. This has the value of the date range which is used to filter the sql query. I am able to handle and modify it while exporting to html however I have a problem in exporting to excel.
The date range comes in the formats below:
- BETWEEN {d '2014-01-01'} AND {d '2014-01-31'}
- IN ({d '2014-01-14'},{d '2014-01-15'}, {d '2014-01-19'} ,{d '2014-01-20'},{d '2014-01-21'})
I like to find out max and min date and display it. However in this case with excel, I am happy with displaying them separated with commas like shown below.
- 2014-01-01, 2014-01-31
- 2014-01-14, 2014-01-15, 2014-01-19, 2014-01-20, 2014-01-21
If I use the basic formula show below, it works in in excel but it does not work when I apply it to excel - formula section of the businessDate element in pentaho report designer.
=TRIM(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(B4,"{d '",""), "BETWEEN", ""), "IN", ""),"'}",", "), " AND ", ""),")",""),"(",))
It does not have to be this way. I am happy with any method suggested to format this raw date range before printing to excel.
Thank you in advance.
After wasting a lot of time, I have found a way which works for all export types. As I said in my question, I was modifying date for html print by using "Structure Tab" --> Select "Master Report" --> "Attributes" Tab --> html -> "append-header" attribute.
<script type="text/javascript">
function init() {
var tableList = document.getElementsByTagName("table");
var businessDate = document.getElementById("businessDatePeriodSelected");
businessDate.innerHTML = businessDate.innerHTML+"testDateModified";
}
window.onload = init;
</script>
This piece of code does the job. However just for html. I needed to come up with something new. I was looking for a solution for excel and pdf exports as well.
HERE IS THE SOLUTION:
Click on "Data" tab next to the "Structure" tab on the right top side. You will see "Functions" in the tree. Right click and hit "Add functions". Select "Script" -->"Bean-Scripting Framework (BSF)". Select function created under Functions. Give it a name and add the code below to the "Expression" section. [This does not need a starting or ending tag]
/* businessdate is one of my parameters that I like to display on the report.
dataRow is automatically recognized by the interpreter which can be used for calling parameter values. It seems like came out of nowhere.*/
String value = dataRow.get("businessdate");
value= value.replaceAll("[^0-9\\-\\{\\}]", "");
value= value.replaceAll("[\\{]", ""); // replace all {
value= value.replaceAll("[\\}]", ","); // replace all }
value= value.substring(0, value.length()-1);
String[] dateArr = value.split(",");
return dateArr[0] +" - "+dateArr[dateArr.length-1];
The last thing you need to do is drag and drop your function somewhere suitable on your report. It will locate a textbox which will display the modified businessdate.
If you like to print a parameter on your pentaho report, this does the job for all exports (html, pdf and excel). You can also modify it before printing. This link pretty helpful as the syntax is slightly different at some points.
good luck.

How to use Excel VBA to extract Memo field from Access Database?

I have an Excel spreadsheet. I am connecting to an Access database via ODBC. Something along then lines of:
Set dbEng = CreateObject("DAO.DBEngine.40")
Set oWspc = dbEng.CreateWorkspace("ODBCWspc", "", "", dbUseODBC)
Set oConn = oWspc.OpenConnection("Connection", , True, "ODBC;DSN=CLIENTDB;")
Then I use a query and fetch a result set to get some table data.
Set oQuery = oConn.CreateQueryDef("tmpQuery")
oQuery.Sql = "SELECT idField, memoField FROM myTable"
Set oRs = oQuery.OpenRecordset
The problem now arises. My field is a dbMemo because the maximum content length is up to a few hundred chars. It's not that long, and in fact the value I'm reading is only a dozen characters. But Excel just doesn't seem able to handle the Memo field content at all. My code...
ActiveCell = oRs.Fields("memoField")
...gives error Run-time error '3146': ODBC--call failed.
Any suggestions? Can Excel VBA actually get at memo field data? Or is it just completely impossible. I get exactly the same error from GetChunk as well.
ActiveCell = oRs.Fields("memoField").GetChunk(0, 2)
...also gives error Run-time error '3146': ODBC--call failed.
Converting to a text field makes everything work fine. However some data is truncated to 255 characters of course, which means that isn't a workable solution.
Try Range.CopyFromRecordset to see if it works.
Try using CStr(oRs.Fields("memoField")) and assign to Value2 of the Range/ActiveCell.
Try making the memo field the last physical column in table. A memo field is read only when retrieved. There was/may-still-be an issue with memo fields that are not at physically at the end of a table.
All I can think of right now.

Resources