how to get date format for excel using xlrd? - xlrd

Our requirement is like to import data from excel and display in web. while importing I m able to convert it into any format(like dd-mm-yy) but requirement is like it should display the same format as defined by user.
So if can get the formatting info, we can format using datetime module pf python.
Thanks in Advance

First verify oledb connection string
conn.ConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Nick\Desktop\Pricing2.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=Yes;IMEX=1;""
To always use IMEX=1 is a safer way to retrieve data for mixed data type columns...
Range rg = (Excel.Range)worksheetobject.Cells[1,1];
rg.EntireColumn.NumberFormat = "MM/DD/YYYY";
this used for validate column date type or not.
Before Save using method in C#
Datetime.TryParseExact();
That method return Datetime ,First leran that method before try it.

Related

Crystal report asks database password and set date format in formula

I have two problems:
When I want printout the Crystal Report, get data from SQL using formula, it always requires database password, can I save my password or open connection string using VB?
When I get the data from database the date format is like 1986-01-01 01:00AM. Can I make it like 1986-01-01 only?
Code:
'CrystalReportViewer1.ReportSource = New CrystalReport3
'CrystalReportViewer1.SelectionFormula = "{PersonalData.IDNo}='" + Information.TextBox18.Text + "'"

ADO recordset fails on "memo" datatype during import into Excel

I am trying retrieve data from a SQL server, for use in some Excel 2003 macros. I would like to avoid the use of QueryTables, as I don't want this intermediate step of writing and reading from actual sheets. It seems time-consuming and pointless.
I have managed to get the recordset, but it contains empty data where the datatype is "memo", on the server.
Further, the program crashes where it tries to store the data into a Range. It appears to make it to the first "empty" field and then it gives me a 1004 Error Code.
Clearly the memo field is giving me grief. Can anyone make a suggestion as to how to get around this, or what I should be doing differently?
objMyConn.connectionString = "ODBC;" _
& "Provider=SQLOLEDB;DRIVER={SQL Server};SERVER=VANDB;" _
& "APP=Microsoft Office 2003;DATABASE=WPDB_BE;Trusted_Connection=Yes;"
objMyConn.Open
I've been searching online for ages, but this Access / ADO / Excel stuff is exceedingly painful. Please help.
Edit 1: I later modified the SQL query with "TOP 1" (SQL version of "LIMIT 1") and found that with that recordset, the memo fields were returned correctly. Similarly, I could SELECT a single problematic field, and get more rows, e.g. "SELECT TOP 52 bad_field FROM ..."
So I suspect that the issue is an ADO connection data size limit of some sort? It seems the Access "memo" type is simply like a "MEDIUMTEXT" MySQL type, so how would I get around such a limit? It's a separate question then, but what alternatives are there to the ADO connections?
You can use your ADO Connection object (objMyConn) to discover the data type (among other attributes) as ADO interprets it:
With objMyConn.OpenSchema(adSchemaColumns, Array(Empty, Empty, "your_table_name_here"))
.Filter = "COLUMN_NAME = 'your_column_name_here'"
MsgBox .Fields("DATA_TYPE").Value
End With
This will return the integer value of its respective SchemaEnum enum value, use the object browser to discover the enum value. Posting the results here could give a further clue to your problem.

VB.NET - Reading a blank string from Excel when trying to read the header of a data column?

A simple though very odd problem. I use an OLEDB connection to read from an excel database, and this loop to read in all of the data from each of the columns
While reader.Read()
For i As Integer = 0 To reader.FieldCount - 1
temp = reader(i).ToString + ControlChars.Tab
output_file.Write(temp)
'output_file.Write(reader(i).ToString() + ControlChars.Tab)
Next
output_file.WriteLine()
End While
Some of the columns contain date information, which are read in fine (usually as a string "2/20/2011" or so), but the headers of those columns are read in as a blank "". The headers for all the other columns read in fine, but not for the date containing columns. Any idea how I can fix this?
Is it because OLEDB is inferring type for the date columns (DateTime or whatever) and the headers to do not conform to this type? I've had similar issues with ODBC ignoring the odd alpha string in a column that is otherwise numeric.
Well here's the solution, which I stumble across accidentally. Your connection string needs "IMEX=1;" in it, which tells the reader that all data types should be read as strings.
Dim jet_string As String = "provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + input_file_path + ";Extended Properties=""Excel 8.0;HDR=No;IMEX=1;"""

csv file reading problem

So I have a csv file:
"453FDG","656HGH","54645","MARIA","V543","534","TRETCITY","R34",09094553,09094553,09094553,"21/01/10","RE"
"45er3FDG","656HGH","54645","M343ARIA","V543","534","TRETCITY","R34",090-94553,0909-4553,090-94553,"21/01/10","RE"
problem 1:
Connection string is this:
Dim strConnString As String = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" & System.IO.Path.GetDirectoryName(filediag.PostedFile.FileName).ToString & ";Extensions=asc,csv,tab,txt;Persist Security Info=False;HDR=NO;IMEX=1"
My problem is when i use this schema.ini, the 9th, 10th and 11th column of the second row of the csv file doesn't read properly if there's a special character in it (it supposed to be telphone number), i think because the row above is returned as a number(integer) because it's pure numeric:
[#42r.csv]:
ColNameHeader= false
Format=CSVDelimited
MaxScanRows=0
CharacterSet=ANSI
So what will I do with this?
problem 2:
Since I can't solve the prob no 1, i tried to use the second connection string:
Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & System.IO.Path.GetDirectoryName(filediag.PostedFile.FileName).ToString & ";Extended Properties='text;HDR=No;FMT=Delimited;IMEX=1"
The problem with this is it treat the first row of the csv file as a column header. Please help. Thanks.
The issue is that you're using Select * FROM CSVFILE.CSV. This is forcing ADO to infer the datatypes, for which it will probably just use the first row.
The best thing to do is probably to follow the schema suggested in this question:
When reading a CSV file using a DataReader and the OLEDB Jet data provider, how can I control column data types?

SQL Query Run Against Excel Workbook Returns Truncated Text Field

I'm running a SQL SELECT query through an ADO connection to an Excel 2007 workbook with the following code (using a custom version of VBScript)
dim ado, rs
set ado = CreateObject("ADODB.Connection")
ado.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=workbook.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX=1"";"
ado.open()
set rs = ado.execute("SELECT * FROM [sheet1$]")
which is straighforward. The problem is that any cell that has text longer than 255 characters is truncated; is there any way around this? Is there a property in the connection string that will support this or is it an option I need to change in the excel document itself? I have tried MSSQL's CAST() function but this just causes an error when executed.
Any help would be greatly appreciated.
I think you're running into a variant of a long-standing limitation in Excel's data access provider. See http://support.microsoft.com/default.aspx?scid=kb;EN-US;189897 for an example or google for thousands more.
Instead of trying to use CAST(), have you tried to use the CONVERT() function?