Reading of Excel files in C #

Source: Internet
Author: User
Excel offers two ways to open an Excel file directly, then read it one line at a time, and one way to read the Excel file as a data source through OLE DB connections
Method One: This method of directly reading cells is important to release.

Excel.Application excel = null;
Excel.Workbooks WBS = null;
Excel.Workbook wb = null;
Excel.Worksheet ws = NULL;
Excel.Range range1 = null;
Object nothing = System.Reflection.Missing.Value;

Try
{
Excel = new Excel.Application ();
Excel. UserControl = true;
Excel. DisplayAlerts = false;

Excel. Application.Workbooks.Open (this. filepath,nothing,nothing,nothing,nothing,nothing,nothing,nothing,nothing,nothing,nothing,nothing,nothing);

WBS = Excel. workbooks;
WB = wbs[1];
WS = (Excel.Worksheet) WB. worksheets["Sheet2"];


int rowcount = ws. UsedRange.Rows.Count;
int colcount = ws. UsedRange.Columns.Count;
if (rowcount <= 0)
throw new Invalidformatexception ("No data record in file");
if (ColCount < 4)
throw new Invalidformatexception ("Wrong Number of fields");

for (int i = 0;i {

This.rowno = i + 1;
object[] row = new Object[4];
for (int j = 0;j<4;j++)
{
Range1 = Ws.get_range (ws. Cells[i+2,j+1],ws. Cells[i+2,j+1]);
ROW[J] = Range1. Value;

if (row[0] = = null)
{
this.isnullrecord++;
Break
}
}

if (This.isnullrecord > 0)
Continue

DataRow DataRow = this.readexcel (row);

if (This.isnullrecord = 1)
Continue

if (This.verifydata (dataRow) = = False)
errflag++;

This.updatetablecurr (DataRow);
}

}
Finally
{
if (Excel!= null)
{
if (WBS!= null)
{
if (WB!= null)
{
if (WS!= NULL)
{
if (range1!= null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject (Range1);
Range1 = null;
}
System.Runtime.InteropServices.Marshal.ReleaseComObject (WS);
WS = NULL;
}
Wb. Close (false,nothing,nothing);
System.Runtime.InteropServices.Marshal.ReleaseComObject (WB);
WB = NULL;
}
Wbs. Close ();
System.Runtime.InteropServices.Marshal.ReleaseComObject (WBS);
WBS = NULL;
}
Excel. Application.Workbooks.Close ();
Excel. Quit ();
System.Runtime.InteropServices.Marshal.ReleaseComObject (Excel);
Excel = null;
Gc. Collect ();
}
}

Method Two: This is the fill dataset, you can return OleDbDataReader to read line by row, the data is faster
Note: This method is easy to read mixed fields as null values, the solution is to transform the connection string
strconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\erp1912.xls; Extended properties= ' Excel 8.0; Hdr=yes;imex=1 ' ";
Through the imex=1 to the hybrid as text-type reading, to avoid null value, please see the details http://www.connectionstrings.com
Private DataSet Importexceltodataset (string FilePath)
{
String strconn;
strconn = "Provider=Microsoft.Jet.OLEDB.4.0 +" + "Data source=" + FilePath + "; Extended Properties=excel 8.0; ";
OleDbConnection conn = new OleDbConnection (strconn);
OleDbDataAdapter mycommand = new OleDbDataAdapter ("SELECT * from [sheet1$]", strconn);
DataSet myDataSet = new DataSet ();
Try
{
Mycommand.fill (myDataSet);
}
catch (Exception ex)
{
throw new Invalidformatexception ("The name of the worksheet for this Excel file is incorrect," + ex.) message);
}
return mydataset;
}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.