关于ASP.NET读取EXCEL数据的问题
我想在客户端读取EXCEL里的数据,并且把数据放到服务器的数据库中。能否实现?怎么实现??谢谢先!!!
[解决办法]
前段时间写的代码,参考一下
把excel读进datatable处理
string excelFilePath = @ "E:\work\Book1.xls ";
Excel.Application myExcel = new Excel.ApplicationClass();
object oMissing = System.Reflection.Missing.Value;
myExcel.Application.Workbooks.Open(excelFilePath, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
Excel.Workbook myBook = myExcel.Workbooks[1];
Excel.Worksheet mySheet = (Excel.Worksheet)myBook.Worksheets[1];
DataTable dt = new DataTable( "ExcelTable ");
dt.Columns.Add( "F1 ", System.Type.GetType( "System.String "));
dt.Columns.Add( "F2 ", System.Type.GetType( "System.String "));
dt.Columns.Add( "F3 ", System.Type.GetType( "System.String "));
dt.Columns.Add( "F4 ", System.Type.GetType( "System.String "));
dt.Columns.Add( "F5 ", System.Type.GetType( "System.String "));
dt.Columns.Add( "F6 ", System.Type.GetType( "System.String "));
dt.Columns.Add( "F7 ", System.Type.GetType( "System.String "));
dt.Columns.Add( "F8 ", System.Type.GetType( "System.String "));
DataSet myDs = new DataSet();
myDs.Tables.Add(dt);
DataRow myRow;
myDs.Clear();
for (int i = 1; i <= 500; i++)
{
myRow = myDs.Tables[ "ExcelTable "].NewRow();
//while (mySheet.Cells[i, j].ToString != " ")
for (int j = 1; j <= 8; j++)
{
Excel.Range r = (Excel.Range)mySheet.Cells[i, j];
string strValue = r.Text.ToString();
string aa = strValue;
string columnname = "F " + j.ToString();
myRow[columnname] = strValue;
//if (strValue == " ")
// Response.Write(i + j);
//j++;
}
myDs.Tables[ "ExcelTable "].Rows.Add(myRow);
//i++;
}
GridView1.DataSource = dt;
GridView1.DataBind();