using System;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using Microsoft.Office.Interop.Excel;
private void Page_Load(object sender,EventArgs e)
{
operateExcel("C:\\EXCEL.xls");
}
private void operateExcel(string pth)
{
DataSet dtset = new DataSet("MYEXCEL");
try
{
//lvContent.Items.Clear();
Workbook wbok = app.Workbooks.Open(pth, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
wbok = app.Workbooks.Open(pth, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
//WorksheetClass wshts = (Worksheets)wbok.Worksheets;
Sheets wshts = wbok.Worksheets;
Worksheet wsht = (Worksheet)wshts.get_Item(1);
System.Data.DataTable dtbl = new System.Data.DataTable("MYEXCEL");
dtset.Tables.Add(dtbl);
DataColumn col = new DataColumn();
col.ColumnName = "COL1";
dtbl.Columns.Add(col);
col = new DataColumn();
col.ColumnName = "COL2";
dtbl.Columns.Add(col);
col = new DataColumn();
col.ColumnName = "COL3";
dtbl.Columns.Add(col);
for (int i = 2; i < 10000 && wasLastnull < 3; i++)
{
Range rnge = wsht.get_Range("A" + i.ToString(), "I" + i.ToString());
System.Array myvalues = (System.Array)rnge.Cells.Value2;
System.Array vals = (System.Array)rnge.Cells.Value2;
string a1 = "", a2 = "", a3 = "" ;
try
{
a1 = vals.GetValue(1, 1).ToString();
a2 = vals.GetValue(1, 2).ToString();
a3 = vals.GetValue(1, 3).ToString();
DataRow dr = dtbl.NewRow();
dr["COL1"] = a1.ToString();
dr["COL2"] = a2.ToString();
dr["COL3"] = a3.ToString();
dtbl.Rows.Add(dr);
count = count + 1;
}
catch (System.Exception ex)
{
Label1.Text = ex.ToString() + "ERROR";
}
}
app.Workbooks.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(wsht);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wbok);
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
}
catch (System.Exception ex)
{
}
}
No comments:
Post a Comment