Home > Net >  C # using the OLEDB reading Excel time, without the second value, how to solve?
C # using the OLEDB reading Excel time, without the second value, how to solve?

Time:11-26



As shown in figure, the filling time is a second value, but after through code access, read within seconds, only to the minutes, such problems can lead to the back of the calculation, have to read the second,
Let users change text format,
Is there any way to solve this problem?

Code:
 public static DataSet ExcelToDS (string file) 
{
//string strConn="Provider=Microsoft. Jet. The OLEDB. 12.0;" + "Data Source=" + Path + ";" + "Extended Properties=8.0 Excel; HDR=NO. IMEX=1 '";
String connstring="";
String filetype=file. The Substring (file. The LastIndexOf ('. ') + 1);
The DataSet dt=null;
If (filetype. ToLower ()=="XLSX")
{
//Office 7 and above version not the presence of redundant space and the semicolon note
Connstring="Provider=Microsoft. ACE. The OLEDB. 12.0. Data Source="+ file +"; Extended Properties='Excel 8.0; HDR=NO. IMEX=1 '";
}
The else//if (filetype. ToLower ()=="XLS")
{
//Office 7 the following version
Connstring="Provider=Microsoft. JET. The OLEDB. 4.0. Data Source="+ file +"; Extended Properties='Excel 8.0; HDR=NO. IMEX=1 '";
}
Using (OleDbConnection conn=new OleDbConnection (connstring))
{

Conn. The Open ();
The DataTable schemaTable=conn. GetOleDbSchemaTable (System) Data. The OleDb. OleDbSchemaGuid. Tables, null);
//string tableName=schemaTable. Rows [0] [2]. The ToString (). The Trim ();
String tableName="sheet1 $";
String strExcel="";
OleDbDataAdapter myCommand=null;

StrExcel="select * from [" + tableName +"] ".

MyCommand=new OleDbDataAdapter (strExcel connstring);
Dt=new DataSet ();
MyCommand. The Fill (dt, "table1");

Conn. Close ();
Return dt.

}

}

CodePudding user response:

Try a different way, using npoi read

CodePudding user response:

reference 1st floor ajdkjalj response:
alternatively, try the npoi read


Using npoi will often appear version does not match, so just change the OLEDB,

CodePudding user response:

Can't you put him in the Excel all display, you Excel in the date to string type a try, if the string is converted to a DateTime types to try
  •  Tags:  
  • C#
  • Related