2015-10-13 294 views
1

我有一個簡單的按鈕事件來將excel表單導入到數據庫中。的碼片/部分是這樣的..在數據庫中插入空的excel單元格作爲空白單元格

private void button6_Click(object sender, EventArgs e) 
    { 
     OpenFileDialog theDialog = new OpenFileDialog(); 
     theDialog.Title = "Open Text File"; 
     theDialog.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm;*.xltm;*.xltx"; 
     theDialog.InitialDirectory = @"C:\"; 
     if (theDialog.ShowDialog() == DialogResult.OK) 
     { 
      try 
      { 
       foreach (var worksheetx in Workbook.Worksheets(theDialog.FileName.ToString())) 
       { 
        foreach (var row in worksheetx.Rows) 
        { 
          String Temp = @"INSERT INTO [dbo].[myTable] 
             ([Status] 
             ,[NName] 
             ,[Notes] 
             ) 
           VALUES 
             ('<Status>' 
             ,'<NName>' 
             ,'<Notes>' 
             )"; 

          String Temp1 = Temp; 
          bool ForceContinue = false; 

         foreach (var cell in row.Cells) 
         {                
          if (cell != null) 
          {         
           if (cell.ColumnIndex == 0)Temp1 = Temp1.Replace("<Status>", cell.Text); 
           if (cell.ColumnIndex == 1)Temp1 = Temp1.Replace("<NName>", cell.Text);         
           if (cell.ColumnIndex == 2)Temp1 = Temp1.Replace("<Notes>", cell.Text);                                      
          } 
          else 
          { 
          //Looking for this part- How to insert 'NULL' or Blank cell.Text 
          } 
         } 

         DBConn.Execute(Temp1);  

例如,如果我的excel表列 - '注意' 是像

| Check | 
|  | 
|  | 

它當前插入DB等

| Check | 
|<Notes>| 
|<Notes>| 

我希望它是這樣的空白插入爲空白

| Check | 
|  | 
|  | 

回答

1

替換內部'foreach(var cell in row.Cells)'中的代碼塊並將其替換爲下面寫的代碼。

if (cell != null) { if (cell.ColumnIndex == 0)Temp1 = Temp1.Replace("<Status>", cell.Text); if (cell.ColumnIndex == 1)Temp1 = Temp1.Replace("<NName>", cell.Text); if (cell.ColumnIndex == 2)Temp1 = Temp1.Replace("<Notes>", cell.Text); } else { Temp1 = "" ; }

1

嘗試結合:運營商,而不是則爲DBNull的if語句。

foreach (var cell in row.Cells) 
{ 
     if (cell.ColumnIndex == 0) Temp1 = Temp1.Replace("<Status>", string.IsNullOrWhiteSpace(cell.Text) ? DBNull.Value : cell.Text); 
     if (cell.ColumnIndex == 1) Temp1 = Temp1.Replace("<NName>", string.IsNullOrWhiteSpace(cell.Text) ? DBNull.Value : cell.Text); 
     if (cell.ColumnIndex == 2) Temp1 = Temp1.Replace("<Notes>", string.IsNullOrWhiteSpace(cell.Text) ? DBNull.Value : cell.Text); 
} 

欲瞭解更多閱讀這裏的一些鏈接。 ?:DBNull

1

第一個問題是,您沒有任何邏輯來捕獲「null」excel單元 - 您只需要捕獲非空單元的代碼。

其次,在這段代碼中一個單元格永遠不會爲空,因爲你在foreach循環中將它實例化爲一個對象。但是,Value,Value2或Text屬性可能爲空白或空白。檢查相應的屬性是否爲空。

取決於你正在嘗試做的,使用此檢查:

if (cell.Value != null) 

if (String.IsNullOrEmpty(cell.Text)) 

,但我不認爲你甚至需要這種檢查,因爲你的更換方法不自動。

第三,也許我有一個不同的Excel互操作的語言,但這並不甚至編譯我:

if (cell.ColumnIndex == 0) 

我能夠使用:

if (cell.Column == 0) 

但是這引發了異常,因爲Excel數字列以1開頭,而不是0.

第四,你有一個非常長的foreach循環來遍歷每一行。試試這個減少處理時間:

foreach (Range row in sheet.UsedRange) 

第五,你不需要每次都實例化字符串Temp。在第一個循環之前移動該代碼。

最後,我只是用這個代碼來替換相應的文本:

  foreach (Range cell in row.Cells) 
      { 
       if (cell.Column == 1) Temp1 = Temp1.Replace("<Status>", cell.Text); 
       if (cell.Column == 2) Temp1 = Temp1.Replace("<NName>", cell.Text); 
       if (cell.Column == 3) Temp1 = Temp1.Replace("<Notes>", cell.Text); 
      } 
相關問題