creating excel template for vb.net export - vb.net

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 !

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?

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

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

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.

How to use VB to create Excel style and format data sheet?

I want to write a program which will replace my current paper based record. My current paper record is basically many column and rows with different width, height, and other properties. I know how to write a VB program that can save the information, but I don't know how to make the VB program to generate a xls datasheet to which would exactly like my paper record.
Would someone please give me the information about that?
Thanks :)
I would recommend http://epplus.codeplex.com/releases/view/42439.
It is very easy to use and integrates flawlessy in vb.net.
I am not providing code as a sample because the samples which are included in the package are very good.
As a hint: Internally I would use a Data-Table to store your values and then use a separate module to load/store it to excel.
An excel file could be thougth as a simple database where each sheet is a different table.
Assuming you have Excel on your machine, you could create an empty XLS file and then use OleDB to fill the worksheets.
Sub WriteToExcel()
Dim con As String con = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\temp\test.xls;" & _
"Extended Properties='Excel 8.0;HDR=No;'"
Using c as OleDbConnection = new OleDbConnection(con))
c.Open()
Dim commandString as String = "Insert into [Sheet1$] (F1, F2, F3) " & _
"values('Column1Text', 'Column2Text', 'Column3Text')"
Using cmd As OleDbCommand = new OleDbCommand(commandString))
cmd.Connection = c
cmd.ExecuteNonQuery()
End Using
End Using
End Sub
other options include OpenXml (which I'd have thought is the "recommended" way to do it but which brings with it a learning curve) or at the other end of the scale (in terms of crudeness) write your data in a comma-separated (csv) format and manually import it into Excel

Problem reading dBase DBF with non-English characters

I have a tool which reads dBase files and uploads the contents to SQL Server, part of a system to import shapefiles. It works but now we have a requirement to import files that include non-English characters (Norwegian in this case, could be other languages later) and they're being corrupted.
The dBase files are being read using an OleDbDataAdapter. Stepping through the code I can see that the text is wrong as it is read in. I'm assuming it's something to do with code pages or Unicode but I have no idea how to fix it.
A dBase Reader application tells me the DBFs are in code page 1252 - I don't know if this is correct. My upload tool runs on Win7 with English (UK) regional settings.
Examples:
ÅSGARD in DBF becomes +SGARD in VB.Net & SQL Server.
RINGHORNE ØST in DBF becomes RINGHORNE ÏST in VB.Net & SQL Server.
The code that reads the DBF:
dbfConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strPath & ";Extended Properties=dBASE IV"
Cnn.ConnectionString = dbfConnectionString
Cnn.Open()
strSQL = "SELECT * FROM [" & strDBF & "]"
DA = New OleDb.OleDbDataAdapter(strSQL, Cnn)
DS = New DataSet
DA.Fill(DS)
If DS.Tables(0).Rows.Count > 0 Then
dtDBF = DS.Tables(0)
Else
dtDBF = Nothing
End If
Data is read like: Name = dtDBF.Rows(index)("NAME_1")
Is there a way to tell OleDbDataAdapter what code page to use or a better way to read dBase files from VB.Net?
Try adding this to your DSN:
CollatingSequence=Norwegian-Danish
You might also be able to use:
CollatingSequence=International
Check whether the shapefile contains codepage information. There are two places to look
Look in the language driver ID (LDID), which is found in the header of the shapefile’s DBF table (in the 29th byte).
Look for an associated separate file with extension .cpg.
If the code page is not specified in those locations, it defaults to the codepage on the PC that generated the shapefile. You will just have to know that :(
I've never used it, but maybe Shape2SQL takes care of this for you? Or shp2text? I believe the PostGIS shapefile loader handles code pages: maybe you could import into PostGIS and then export in another format??
Old question, but this may answer it for future readers...
You might try adding a property setting in your connection string:
Locale Identifier=1044
This property (and a list of values including this one) is documented for ADO in conjunction with Jet 4.0's OLDB Provider but I have no reason to believe it isn't also supported by ADO.Net. This value (1044) is Norwegian/Danish.
Untested, but something else to try.