Excel cannot open the file because the file format or file extension is not valid - PHP Maatwebsite - laravel

In our backend project, we have some features that allows to export files to xlsx.
We have done some tests locally and everything is working fine.
However, I deployed the app in an Azure App Service and some of the exports (not all) are not working fine.
I deployed backend (PHP), frontend (React) and database (MySQL) in Azure. I copied the information of the local DB to the DB deployed in azure (just because we don't wanna waste time with the data).
In the backend project, we are using Laravel with Maatwebsite for the features related to the exports. I created a class that implements FromQuery, WithHeading and ShouldAutoSize and that has construct, query and headings functions. Next, in a Controller, I use this structure to get the file:
public function functionName(Request $request){
$fileName= 'name'.time().'.xlsx';
return (new ClassThatHasTheLogicOfExporting($request->all()))->download($fileName);
}
All my exports works like that, but I'm getting the following error in some of them:
"Excel cannot open the file because the file format or file extension is not valid"
When I try to open the file.
I've been reading about the issue and some people says that I should use ob_start() (at the beggining of the class) and ob_end_clean() (just before exporting the file) but when I do this, I get a 404 error in the web app.
A curious thing is that when I add a filter to the data for exporting (for instance, downloading only the data of people in a specific city) (I can do it in the frontend app), the file opens without errors.
With this strategy, I've achieved downloading all the data in separate groups, so I don't think that the issue is related to special characters in the data.
Does anybody have any suggestion?
I'm using maatwebsite/excel 3.1 and PHP 8 with Laravel 8

I believe the error statement correctly suggests the error is being caused due to an invalid extension. Don’t worry there are several workarounds to this, I am sharing them below:
Method 1 – Change the Default File Format to Save Excel Workbooks
In Excel 2007, click the ‘Microsoft Office’ button and then ‘Excel Options’. In Excel 2010 and higher versions, click File > Options.
Select the Export option.
Select the Change File Type option.
Change the file extension, and then click Save As.
Method 2 – Recover Unsaved Workbook
Go to File and select the Info option.
Under Manage Versions, select ‘Recover Unsaved Workbooks’ op
If MS Excel has unsaved files then, it will list them. You can open and save it.
Method 3 – Use the' Open and Repair' Feature of MS Excel
Open MS Excel application.
Go to File and select the Open option.
Select the corrupt file and choose the Open and Repair option.
You can also check this forum:
https://social.technet.microsoft.com/forums/office/en-US/63020ccc-51d7-46d9-b956-121c0e6efcc8/excel-file-error-the-file-format-and-extension-dont-match?forum=Office2016ITPro
Best Regards,
Steve

you can add the below
if (ob_get_length() == 0 ) {
ob_start();
$response = Excel::download(new KingdomsExport, 'kingdoms.xlsx',\Maatwebsite\Excel\Excel::XLSX);
return $response;
}

Related

Oracle Apex: Is it possible to change the default schema at runtime?

We have an Apex application (version 20.1) and our users must be able to change the database schema at runtime via button click (preferably without logging in again).
Currently we are solving this by installing our application multiple times, once per schema.
We recently discovered the function apex_export.get_application. We intend to use this function to bring our frontend under version control (finally!). We would like to deploy our application directly from the exported files. Having a single application, we would not have to mess with the internal component ids from the exported files.
Is it possible to install the application once and change the default schema via Pl/SQL code? Thank you!
I don't think this can be done, but perhaps the following is a reasonable compromise
add all the schemas you need to support to the workspace schema list
Any SQL (and I do mean any) in your app would be prefixed with an application item, eg
Before: select * from my_table
After: select * from &my_schema..my_table
At login time (or when a user selects it) you modify the MY_SCHEMA application item
(I've not tried this...so test/tread carefully)

Exporting Sonarqube reports into Excel - based on major, minor and critical categories

Is there a way to export Sonarqube reports into Excel - based on major, minor and critical categories?
You can use the REST API, to query the data into JSON text and then export that JSON to a CSV file.
I used the command below to get a JSON response:
http://xxxxx.xx.xxxx.com:9000/api/issues/search?componentRoots=test_xxx_xx&statuses=OPEN,REOPENED&pageSize=500&pageIndex=1
Where componentRoots is the your sonar project name.
It gave all the issues in JSON and then I converted it in to a CSV.
The only way is to use the api/issues/search web service
Yes you can use the REST APIs provided with SonarQube to query. The documentation of APIs is also embedded into every Sonar instance as different versions expose different APIs.
We use Python for similar work as response will be in JSON and it will be easier to manipulate. Once you have arrived extracted issues of your liking, write them into .CSV or excel.
Link to web services will be in footer of Sonar Instance.
PS: Expanded answer to offset short-sighted answering. What is provided here is only abstract and not complete answer with query details.
Not recommended by community
With DB:- If you have been using Sonar for long and if you won't upgrade SQ too often you can choose to study table structure and understand how data is organised. We have done this too, but it gets messier with every passing upgrade (more inner queries). Cost of querying will be saved on bulk process of data as ES is not involved
Please try the following command to get all issues in JSON format. Then you may consider to parse output by using jason parser programs.
Replace "XXX:XXX" with Sonar Key defined in "sonar.projectKey" variable on "sonar-project.properties.txt" file
http://localhost:9000/api/issues/search?componentKeys=XXX:XXX
I used F12 developer tools to create the report. Here is step by step solution:
Before opening Sonar report, press F12 to open developer tools in the browser tab (Refer screenshot). Click on Network tab. Initially it'll be empty:
Open Sonar report in the same browser tab.
Now you have to select a request in URL column (Refer screenshot in point # 4). You've to select that request whose URL is of the format - https://sonar:8443/api/issues/search?. Base URL might be different depending upon the name of the server where you have hosted Sonar website.
Click on Response tab (Refer screenshot):
Copy the entire JSON data to clipboard. First press Ctrl + A and then Ctrl + C to copy the contents.
JSON data that you got is in below format:
Data of all the issues is present inside issues key. It is of array type as it contains a list of issues. Paste the entire JSON data into any notepad editor and get the value of issues node. A sample issues node data is shown below:
[
{
"key":"AYMBNpviy48WWZHdsU1Z"
,"rule":"OWASP:UsingComponentWithKnownVulnerability"
,"severity":"MAJOR"
,"component":"B91661CE-50F8-45CB-8F54-29CD044EF32D"
,"project":"B91661CE-50F8-45CB-8F54-29CD044EF32D"
,"flows":[]
,"status":"OPEN"
,"message":"Filename: jquery-ui-1.10.3.custom.js | Reference: CVE-2021-41184 | CVSS Score: 6.1 | Category: CWE-79 | jQuery-UI is the official jQuery user interface library. Prior to version 1.13.0, accepting the value of the `of` option of the `.position()` util from untrusted sources may execute untrusted code. The issue is fixed in jQuery UI 1.13.0. Any string value passed to the `of` option is now treated as a CSS selector. A workaround is to not accept the value of the `of` option from untrusted sources."
,"author":""
,"tags":["cve","cwe","cwe-937","owasp-a9","vulnerability"]
,"transitions":["confirm"]
,"actions":["set_tags","comment","assign"]
,"comments":[]
,"creationDate":"2022-08-18T06:35:49+0100"
,"updateDate":"2022-08-18T06:35:49+0100"
,"type":"VULNERABILITY"
,"branch":"sonarqubepr"
,"scope":"MAIN"
,"quickFixAvailable":false
},
{
"key":"AYMBNpviy48WWZHdsU1_"
,"rule":"OWASP:UsingComponentWithKnownVulnerability"
,"severity":"MAJOR"
,"component":"B91661CE-50F8-45CB-8F54-29CD044EF32D"
,"project":"B91661CE-50F8-45CB-8F54-29CD044EF32D"
,"flows":[]
,"status":"OPEN"
,"message":"Filename: jquery.ui.datepicker-ml.min.js | Reference: CVE-2021-41183 | CVSS Score: 6.1 | Category: CWE-79 | jQuery-UI is the official jQuery user interface library. Prior to version 1.13.0, accepting the value of various `*Text` options of the Datepicker widget from untrusted sources may execute untrusted code. The issue is fixed in jQuery UI 1.13.0. The values passed to various `*Text` options are now always treated as pure text, not HTML. A workaround is to not accept the value of the `*Text` options from untrusted sources."
,"author":""
,"tags":["cve","cwe","cwe-937","owasp-a9","vulnerability"]
,"transitions":["confirm"]
,"actions":["set_tags","comment","assign"]
,"comments":[]
,"creationDate":"2022-08-18T06:35:49+0100"
,"updateDate":"2022-08-18T06:35:49+0100"
,"type":"VULNERABILITY"
,"branch":"sonarqubepr"
,"scope":"MAIN"
,"quickFixAvailable":false
}
]
The JSON array data obtained in previous step is of real use. Convert this JSON array data into an EXCEL file using any online converter. I used the following website - https://www.convertcsv.com/json-to-csv.htm
What if my report has more than one page?
Sonar loads 100 records per page. To get the next page you've to click on Show More button at the bottom:
Every time you click on the Show More button, a new request goes to the Sonar server which will show up in the F12 developer tool's Network tab. So for all subsequent pages, repeat same set of steps to create a new Excel sheet per page. Then merge all the Excel sheets manually to create single master report.
Note: While merging the Excel files, remember that every Excel file will have a header row at the top. While merging the files, you'll have to keep the header row from from the first Excel sheet and ignore from the rest.

Microsoft office cannot find the input table or query

I am trying to migrate an access database from 2000 to 20007 version. I have two .mdb files one used as a front end and the other containing only the back end tables. I performed all the necessary initialization for the migration like export of table, extension with .accd, etc.. But when i execute the following code , i get an error " Microsoft office cannot find the input table or query . Check spelling and so".
set Db=currentDb
SQLText ='some sql query'
Db.OpenRecordset(SQLText, dbOpenDynaset)
When i tried to output the value of Db with a msgbox (MSgbox Db), it shows the message "ABC, type mismatch", where ABC is the function name. But, Msgbox Db.name showing the correct database name. But, the same code has been running fine on the older version.
Please help me on this
Okay, you do not have to "migrate" to a different version persay. You only have to import everything from an MDB to ACCDB file. That is all. Scratch whatever you have done, start afresh.
Create a new "empty" accdb file, then using the options under
External Data-> Import/Link From Access. Select the Front End Access
file. Use the Option IMPORT Forms, Report etc., select all Forms, Reports, Queries, Mods then click OK. This
will import all objects.
Create a new "empty" accdb file, then using the options under
External Data-> Import/Link From Access. Select the Back End Access file. Use the Option IMPORT Forms, Report etc., select all tables and click OK. This will import all the tables over to the new file.
Now open up your new "front end" (the one with accdb extension). Under External Data -> Import/Link. Select the new "back end" (the one with the accdb extension). Use the Link to the Data Source option. Then select all tables and click OK.
Now perform a Compact & Repair, follow it with a Compile of the VBA code.
The whole strategy will fail if you have a backend DB in a different platform like SQL or Oracle.

Can I export a report design layout from Access?

I'm converting an application from access to c#. I have a adp file for the access application. I've only been able to export the report as a .cls file, which does nothing for me. Is there a way to export the report design view so that I don't have to redo it from scratch??
Thanks!
Are you able to use the SaveAsText method to export your report e.g:
saveastext(acReport,"ReportName","FileName.txt")
And then re-import the report using the LoadFromText method e.g:
loadfromtext(acReport,"ReportName","FileName.txt")
This approach allows to export elements from MS Access to a text file containing all the file attributes etc and then allows you to load it back in keeping all it's settings/configurations

How to write a datatable to excel work book at client side

Currently i am doing one project in that we need to generate report from database.
Since my server memory is too low im getting 'Out of Memory' Exception when im writing it at serverside and also when i write directly to a excel file using http header as excel file im not able to create multiple sheets since my database table is huge more than 65536 rows.
I saw many solution using a third party tool but i cant use those into mine..If anyone already worked on this please give me some direction.
Also i tried using javascript but for that i need to use datagrid at server side??
but in my project i m not allowed to use like this.
You can open an excel file, as an xml document using the Open XML format SDK: http://msdn.microsoft.com/en-us/library/bb448854.aspx

Resources