方式一:用微软提供的Microsoft.Office.Interop.Excel这个DLL,程序读取Excel数据,代码如下:
Excel.Application m_ExcelFile = new Excel.Application();
Excel._Workbook m_Workbook;
Excel._Worksheet m_Worksheet;
object missing = System.Reflection.Missing.Value;
Console.WriteLine("excelFilePath:" + excelFilePath);
m_ExcelFile.Workbooks.Open(excelFilePath, missing, missing, missing, missing, missing, missing, missing, missing, missing
, missing, missing, missing, missing, missing);
m_ExcelFile.Visible = false;
m_Workbook = m_ExcelFile.Workbooks[1];
m_Worksheet = (Excel.Worksheet)m_Workbook.ActiveSheet;
int clomn_Count = m_Worksheet.UsedRange.Columns.Count;
int row_Count = m_Worksheet.UsedRange.Rows.Count;
for (int i = 2; i < row_Count + 1; i++)//
{
string lv_strSql;
string lv_strSqlOne = "insert into user (";
string lv_strSqlTwo = " value(";
for (int j = 1; j < clomn_Count + 1; j++)
{
if (((Excel.Range)m_Worksheet.UsedRange.Cells[1, j]).Text.ToString() == "会员姓名" && ((Excel.Range)m_Worksheet.UsedRange.Cells[i, j]).Text.ToString().Trim() != "")
{
lv_strSqlOne += "name,";
lv_strSqlTwo += "'" + ((Excel.Range)m_Worksheet.UsedRange.Cells[i, j]).Text.ToString() + "',";
}
.........//表格可能有好多列
else if (((Excel.Range)m_Worksheet.UsedRange.Cells[1, j]).Text.ToString() == "累计积分" && ((Excel.Range)m_Worksheet.UsedRange.Cells[i, j]).Text.ToString().Trim() != "")
{
lv_strSqlOne += "score,";
lv_strSqlTwo +