需求:根據Excel某行標注了黃色高亮顏色,說明該行數據已被用戶選中(Excel文件中并沒有“已選中”這一列,純粹用顏色表示),導入數據到數據庫時標注此行已選中
直接上代碼:
//選擇Excel文件private void btnBrowse_Click(object sender, EventArgs e){using (OpenFileDialog openFileDialog = new OpenFileDialog()){//openFileDialog.InitialDirectory = "c:\\";openFileDialog.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm";openFileDialog.Title = "Select an Excel File";if (openFileDialog.ShowDialog() == DialogResult.OK){txtFilePath.Text = openFileDialog.FileName;}}}//上傳Excel文件(判斷單元格背景色)
private DataTable ReadExcelToDataTable(string filePath)
{DataTable dataTable = new DataTable();ExcelPackage.LicenseContext = LicenseContext.NonCommercial;using (ExcelPackage package = new ExcelPackage(new System.IO.FileInfo(filePath))){// 獲取第一個工作表ExcelWorksheet worksheet = package.Workbook.Worksheets[0];// 獲取最大列數和行數int rowCount = worksheet.Dimension.Rows;int columnCount = worksheet.Dimension.Columns;// 創建列for (int col = 1; col <= columnCount; col++){string columnName = worksheet.Cells[1, col].Value?.ToString() ?? $"Column{col}";dataTable.Columns.Add(columnName);}// 添加數據行(從第2行開始,第1行是標題)for (int row = 2; row <= rowCount; row++){DataRow dataRow = dataTable.NewRow();for (int col = 1; col <= columnCount; col++){var cell = worksheet.Cells[row, col];if ((col - 1) == 0)//第一列{var BackgroundColor = cell.Style.Fill.BackgroundColor.LookupColor();//單元格背景顏色:#FFFFFF00黃色;#FFFFFF白色 if (!string.IsNullOrWhiteSpace(BackgroundColor)){dataRow[col - 1] = BackgroundColor;//獲取單元格背景顏色}else{dataRow[col - 1] = "#FFFFFF";//純白色}}else//第二列.....N列,Excel數據列{dataRow[col - 1] = worksheet.Cells[row, col].Value?.ToString() ?? "";//數據}}dataTable.Rows.Add(dataRow);}}return dataTable;
}//處理Excel的數據(節選)
private void btnImport_Click(object sender, EventArgs e)
{if (!string.IsNullOrWhiteSpace(txtFilePath.Text) && File.Exists(txtFilePath.Text)){System.Data.DataTable dt = ReadExcelToDataTable(txtFilePath.Text);//讀取excelif (dt != null && dt.Rows.Count > 0)//有數據{for (int i = 0; i < dt.Rows.Count; i++){//...略...if (dt.Rows[i][0]?.ToString() == "#FFFFFF00")//判斷顏色代碼,黃色{u.Winningbidder = dt.Rows[i][6]?.ToString();//添加選中行數據}else{u.Winningbidder = null;//不添加數據}u.WinningbidderColor = dt.Rows[i][0]?.ToString();//保存顏色代碼//...略...}//...略...}//...略...}
}//根據條件替換整行背景顏色
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{if (e.RowIndex > -1){string WinningbidderColor = this.dataGridView1.Rows[e.RowIndex].Cells["WinningbidderColor"].Value.ToString();//背景色代碼 string ID = this.dataGridView1.Rows[e.RowIndex].Cells["ID"].Value.ToString();if (WinningbidderColor == "#FFFFFF00"){this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = ConvertToColor(WinningbidderColor);//整行顏色}else if (ID == "")//合計{this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = ConvertToColor("Red");//整行顏色this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = ConvertToColor("#FFFFFF");//字體顏色}else{this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = ConvertToColor("#FFFFFF");//#FFFFFF 白色}}
}// 通用方法:支持 "#RGB", "#RRGGBB", "#ARGB", "#AARRGGBB", "Red" 等格式 public static System.Drawing.Color ConvertToColor(string colorCode){if (string.IsNullOrEmpty(colorCode))return System.Drawing.Color.Empty;// 處理 HTML 格式if (colorCode.StartsWith("#")){try{return System.Drawing.ColorTranslator.FromHtml(colorCode);}catch{// 忽略異常,繼續嘗試其他格式}}// 處理 RGB 整數格式(如 "255,0,0")if (colorCode.Contains(",")){var parts = colorCode.Split(',');if (parts.Length == 3){return System.Drawing.Color.FromArgb(int.Parse(parts[0]),int.Parse(parts[1]),int.Parse(parts[2]));}else if (parts.Length == 4){return System.Drawing.Color.FromArgb(int.Parse(parts[0]),int.Parse(parts[1]),int.Parse(parts[2]),int.Parse(parts[3]));}}// 處理顏色名稱或其他格式return System.Drawing.Color.FromName(colorCode);}
核心代碼:
var BackgroundColor = cell.Style.Fill.BackgroundColor.LookupColor();//返回單元格背景色
圖例:
Excel導入前
Excel導入后