NET与EXCEL的有效导入代码

   Excel.ApplicationClass exl=new Excel.ApplicationClass();
   Excel.Workbook wb = exl.Workbooks.Open(
    sExcelFileName,
    System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
    System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
    System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing
    );
   Excel._Worksheet ws=(Excel._Worksheet)wb.Worksheets[sSheetName];
   System.Data.DataTable NewDataTable=new System.Data.DataTable("TableData");
   NewDataTable.Columns.Add("Cell");
   NewDataTable.Columns.Add("Shift");
   NewDataTable.Columns.Add("Manufacture_Order");
   NewDataTable.Columns.Add("Line");
   NewDataTable.Columns.Add("Customer Order");
   NewDataTable.Columns.Add("Item No");
   NewDataTable.Columns.Add("SIZE");
   NewDataTable.Columns.Add("Plan_Qty");
   NewDataTable.Columns.Add("Plan_Time");
   NewDataTable.Columns.Add("Plan_OT");
   NewDataTable.Columns.Add("UNBUDGET_HR");
   NewDataTable.Columns.Add("UNBUDGET_RSN");
   NewDataTable.Columns.Add("REMARK");

   int nRowi=7;
   int nClnj;
   double nQty;
   string sCell;
   while (true)
   {
    System.Data.DataRow myRow=NewDataTable.NewRow();
    sCell=((Excel.Range)ws.UsedRange.Cells[nRowi,2]).Text.ToString().Trim();
    if (sCell=="")
    {
     break;
    }
    for (nClnj=2;nClnj<15;nClnj++)
    {
     sCell=((Excel.Range)ws.UsedRange.Cells[nRowi,nClnj]).Text.ToString().Trim();
     if (nClnj==8 || nClnj==9 || nClnj==10 || nClnj==11 || nClnj==12)
     {
      if (sCell=="")
       myRow[nClnj-2]="0" ;
      else
       myRow[nClnj-2]=sCell ;
      try
      {
       nQty=Convert.ToDouble(myRow[nClnj-2]);
      }
      catch
      {
       nClnj++;
       Console(nClnj+"列数据类型非数字型!");
       return null;
      }
     }
     else
     {
      myRow[nClnj-2]=sCell ;
     }
    }
    nRowi++;
    NewDataTable.Rows.Add(myRow);
   }
   ws=null;
   wb.Close(Type.Missing ,Type.Missing,Type.Missing);
   exl.Quit();
   wb=null;
   exl=null;
   if (nRowi==7) NewDataTable=null;
   return NewDataTable;

阅读更多
文章标签: excel null string
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

NET与EXCEL的有效导入代码

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭