How to process excel file in vb.net without office installed - vb.net

I am fairly new to VB.net and never used it for processing Office files.
Right now I have to look at Excel file and send some emails based on the data in the cells. I do not need to write anything to these files.
So far I have read quite a bit about PIAs and so far it looks that I have to design my application for particular Office version?
Is there a way to write the application which could handle files created by different versions of MS Excel?
I would like to be able to process these files without Office being installed on the computer at all, is there any way to do it?

You could use Open XML SDK 2.0 for Microsoft Office link

Don't need to install Office for getting data from excel file.
But for getting data from EXCEL fileyou have to install OLEDB driver in your local machine as well as server if you are hosted your application on server.
You can download OLEDB driver from MICROSOFT.
con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + YourEXCELFilePath + ";Extended Properties=Excel 12.0;")
con.Open()
atatable = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
sheetname = datatable.Rows(0)("table_name").ToString
excelcomm = New OleDbCommand("select * from [" + sheetname + "]", con)
adexcel = New OleDbDataAdapter(excelcomm)
adexcel.Fill(Dataset)
After this code you will get excel sheet data in dataset.
Might this code can help you to get data from excel file.
(NOTE:this code is in vb.net)

you can use
npoi
epplus
which do not require excel

Related

HOW to finalize my vb.net and work without error in MS access database destination

i have three paths/destinations in my vb.net project, i want to make it work in all computers i add my program in it.
my database destination was
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\sh\Desktop\TEST\Database.accdb
after i compiled and build the project, it works
BUT after i copy the program in another computer it doesnt work.
then i changed my destination to...
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Database.accdb"
also i tried
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\Database.accdb"
BUT THEY ARE NOT WORKING
my other destination for open my DGV in PDF "im using iTexSharp"
Dim fontArialBold As BaseFont = BaseFont.CreateFont("c:\windows\fonts\arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED)
Dim folderPath As String = "C:\Users\Sha\Desktop\"
also can the program work in a computer doesn't have VB.net or MS access?

creating excel template for vb.net export

Is there any way that I could create a template in excel for exporting data in vb.net?
I already created a code that will export data to excel from vb.net and create an excel design to display the data..However, when it comes to execution and exporting of data, the system is taking too long to process the command and ultimately won't respond and hangup.
Any idea if maybe there is a way that I could just create a template so the system only needs to send in the data to the template?
You can do it like this (I made template file with office 2003):
= Create xls file that you will use as a template master, i.e: Template.xls
= Copy the file to any .xls name each time you do export, i.e: data1.xls
My.Computer.FileSystem.CopyFile("Template.xls", "data1.xls", _
FileIO.UIOption.AllDialogs, FileIO.UICancelOption.DoNothing)
= Then you will use data1.xls as working database
Dont forget ..
Imports System.Data.OleDb
So the code ...
Dim cnXLS As Data.OleDb.OleDbConnection
Dim cmdXLS As Data.OleDb.OleDbDataAdapter
cnXLS = New OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;" & _
"data source= data1.xls ;Extended Properties=Excel 8.0;")
cmdXLS = New OleDbDataAdapter("select * from [Sheet1$]", cnXLS)
'Codes as you need here
Hope this help you !

Is it possible to automatically repair corrupt Excel workbooks?

I am using SPSS 15 to create several Excel reports which I am then consolidating using an Excel macro. Unfortunately that particular SPSS version produces .xls files that aren't easily readable for Excel 2007 and up. Excel 2003 gobbles those files up just fine, but newer Excel versions display two error messages. First up is "Excel found unreadable content in filename.xls. Do you want to recover the contents of this workbook?". After clicking yes this is followed by "File Error: data may have been lost". Unfortunately these error messages cause my macro to quit at the first file with error code 1004. This is the code I am using to open my Excel files:
If Len(Dir(ThisWorkbook.Path + "\filename.xls")) <> 0 Then
Workbooks.Open Filename:=ThisWorkbook.Path + "\filename.xls"
End If
I checked with IBM (SPSS vendors) and they told me that this particular issue is fixed in SPSS 16 but because of business reasons an upgrade is not on the cards. Now there is a manual workaround which involves opening the files and saving again but with dozens of files that's obviously no fun. Therefore I am looking for a way to automatically repair those corrupt workbooks in my macro.
Further information: we are using Excel 2010 at work, Excel 2003 is not available. A sample file is available here: https://onedrive.live.com/?cid=52106BC267261CBF&id=52106BC267261CBF!292
That file seems fairly screwed up, the BIFF validation tool reports an incorrect BOF header so its impressive Excel 2003 can open it at all.
I can read your file as an ISAM database via the Jet OLEDB provider so you may choose to read the file this way (or use this approach to generate a CSV file for processing)
Dim cn As Object, rs As Object
Set cn = CreateObject("ADODB.Connection")
cn.Open "Provider=MICROSOFT.JET.OLEDB.4.0;Data Source=C:\temp\DebugFile.xls;Extended Properties=""Excel 8.0;HDR=Yes;"""
Set rs = cn.Execute("SELECT * FROM [DebugFile$]")
Do While Not rs.EOF
Debug.Print rs.Fields(0).Value
rs.MoveNext
Loop

Import data from Excel to SQL Server 2008

I'm doing a redesign on a site that uses VB.Net. The system will get a fair bit of data from Excel 97-2003 files, which are uploaded by a third party, then uploaded to SQL 2008. Here's the problem, the files that are uploaded have the extension .P. I've used the below code to try and grab the data and upload to the database.
Dim xlApp As New Excel.Application
xlApp.Workbooks.Open(Filename:=Server.MapPath("~/ExtractFiles/10-31-13.P"))
xlApp.ActiveWorkbook.SaveAs(Filename:=Server.MapPath("~/ExtractFiles/10-31-13.xls"), FileFormat:=51)
If Not xlApp Is Nothing Then
xlApp.ActiveWorkbook.Close()
xlApp.Quit()
xlApp = Nothing
End If
PrmPathExcelFile = Server.MapPath("~/ExtractFiles/10-31-13.P")
plmExcelCon = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + PrmPathExcelFile + ";Extended Properties=""Excel 12.0;HDR=YES;IMEX=1""")
cmdLoadExcel = New System.Data.OleDb.OleDbDataAdapter("select * from [10-31-13]", plmExcelCon)
Dim dt As System.Data.DataTable
dt = New System.Data.DataTable
cmdLoadExcel.Fill(dt)
plmExcelCon.Close()
When the code hits the line cmdLoadExcel.Fill(dt), I'm only getting an error that says "External table is not in the expected format.". I'm assuming that this has to do with the fact that I'm trying to change the file extension. However, I can't seem to open the file with the extension .P.
Is there a method I'm overlooking here? Or is this just not possible when working from a file with a custom extension.
Alright, I found the answer thanks to the hint by varocarbas. For future reference, double check the OledbConnection against the Excel file type. After I put in the following changes it worked:
plmExcelCon = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + PrmPathExcelFile + ";Extended Properties=""Excel 8.0;HDR=YES;IMEX=1""")
cmdLoadExcel = New System.Data.OleDb.OleDbDataAdapter("select * from [10-31-13$]", plmExcelCon)
First, to work with my version of Excel, I needed to use ACE.OLEDB.12.0 and Excel 8.0. Second, I had to add a simple '$' to the end of the table select, otherwise it wouldn't be able to find the right worksheet.

Error while uploading Excel data to SQL server 2005

I have a requirement to upload excel data into a SQL Server 2005 table. Initially I copied Excel data to a temp table and based on condition I have updated and inserted records into the SQL Server table.
Everything works fine in my local system. Same program I moved to quality server there I faced this error
The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.
Then we installed 'Microsoft.ACE.OLEDB.12.0 provider on quality server. Now this error appearing
The Microsoft Office Access database engine cannot open or write to the file ''. It is already opened exclusively by another user, or you need permission to view and write its data."
Kindly help me out to solve this issue.
SqlConnection con = new SqlConnection();
con = SQLManager.openSQLConnection();
string path = FileUpload1.PostedFile.FileName;
string excelConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0 Xml;Persist Security Info=False";
//Create Connection to Excel work book
OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
OleDbCommand cmd = new OleDbCommand("Select [CM_CODE],[CM_NAME],[CM_ADD1],[CM_ADD2],[CM_ADD3],[CM_ADD4],[CM_CITY],[CM_PHONE],[CM_CG_CODE],[CM_CO_CODE],[CM_EXPIRY_DATE],[CM_PINCODE],[CM_EMAIL] from [Sheet1$]", excelConnection);
excelConnection.Open();
OleDbDataReader dReader;
dReader = cmd.ExecuteReader();
Regards,
Sathya
This is most likely a file permissions issue. Does the account that the program runs under have folder level permission to read files from the file location.
Find out which account the program runs under and then add appropriate access permissions to the folder where you upload your excel files.
If it's IIS then whichever account your app pool runs under needs write access to the folder that you upload to. Permissions can usually only be set by web server admin.
Wing
Finally i found out the solution.
I have created one folder under my application folder in Quality server.
Uploaded excel sheets wil get stored under this new folder.
and that path i am referring while updating my SQL server database.
Everything works fine now...
Thank u all for your support
Regards,
Sathya