我有個複選紐,欄位名稱叫COMPLICATION

我點選ABC的話COMPLICATION欄位則顯示 a,b,c
目前我需要把COMPLICATION做分割並放入對應欄位裡
sheet.Cells[rowIdx, 1].Value = ;
sheet.Cells[rowIdx, 2].Value = ;
sheet.Cells[rowIdx, 3].Value = ;
sheet.Cells[rowIdx, 4].Value = ;
sheet.Cells[rowIdx, 5].Value = ;
sheet.Cells[rowIdx, 6].Value = ;
sheet.Cells[rowIdx, 7].Value = ;
sheet.Cells[rowIdx, 8].Value = ;
sheet.Cells[rowIdx, 9].Value = ;
sheet.Cells[rowIdx, 10].Value = ;
sheet.Cells[rowIdx, 11].Value = ;
就假設說COMPLICATION的值是 a,b,c那結果就是
sheet.Cells[rowIdx, 1].Value = a;
sheet.Cells[rowIdx, 2].Value = b;
sheet.Cells[rowIdx, 3].Value = c;
只是目前我除了能做字串分割外後續暫時沒想到,請問這個問題該如何解呢?
更新
我後來是用把他寫進LIST裡面並跑foreach迴圈做判斷
 var splite = item.COMPLICATION.Split(',');
 var splitelist = new List<ReportViewModel>();
 foreach (var stringcode in splite)
 {
     splitelist.Add(new ReportViewModel() { text = stringcode });
 }
 
 foreach (var splitelisrcode in splitelist)
 {
  if (splitelisrcode.text == "a") ...
 }
大概是這樣
string[] Array = complication.split(",");
sheet.Cells[rowIdx, 1].Value = array[0];
sheet.Cells[rowIdx, 2].Value = array[1];
sheet.Cells[rowIdx, 3].Value = array[2];
我不知道你在C#的經驗有多久, C#的陣列元素都是由0開始的.
另外, 如果勾選的是a, b, d, Complication字串中沒有c, 這樣子你要如何處理呢?
可以嘗試做的方法想到2種
把有勾和沒勾的都串起來, 假設全部選項是a,b,c,d,e 5種,你選了a和d,因此字串變成"a,,,d,", 因此字串切割逗號會變成["a", "", "", "d", ""]
選項背後的value多加從0開始的index, 假設全部選項是a,b,c,d,e 5種,而value改成
0:a,1:b,2:c,3:d,4:e,你選了a和d, 得到字串0:a,3:d,先用逗號切割、再用分號切割,可得到a和d對應的0和3 index
前端多增加一個beforeSubmit來轉換所有的checkbox做成一個可被後端split的字串
下例我沒使用jQuery或其他框架,只用pure Javascript
這樣有勾沒勾都會被正確的切割成完整的數量了
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="myproject_test"%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script>
        var beforeSubmit = function (e) {
            var AllTest = document.getElementsByName("cbTest[]");
            var newReq = [];
            for (i = 0; i < AllTest.length; i++) {
                newReq.push(AllTest[i].checked ? AllTest[i].value : "");
            }
            document.postdataform.cbTestArray.value = newReq.join(',')
            document.postdataform.submit();
        }
    </script>
</head>
<body>
    <form method="post" name="postdataform" action="test.aspx">
        <input type="checkbox" name="cbTest[]"  value="a" />a
        <input type="checkbox" name="cbTest[]"  value="b" />b
        <input type="checkbox" name="cbTest[]"  value="c" />c
        <input type="checkbox" name="cbTest[]"  value="d" />d
        <input type="checkbox" name="cbTest[]"  value="e" />e
        <input type="checkbox" name="cbTest[]"  value="f" />f
        <input type="hidden" name="cbTestArray" value="" />
        <input type="hidden" name="act" value="test" />
        <input type="button" value="送出" onclick="beforeSubmit();" />
    </form>
</body>
</html>
後端C#用Split把前端送來的checkbox串成的字串拆開處理
var splitedData = Request.Form["cbTestArray"].Split(new char[] { ',' });
for(int i=0; i < splitedData.Length; i++)
{
    Response.Write(string.Format("request[{0}] => {1}<br>",i, splitedData[i]));
}
(今天又鬧笑話了)
試試看利用ASCII code做為Index
var splite = item.COMPLICATION.Split(',');
 
 foreach (var splitelisrcode in splitelist)
 {
  char code =splitelisrcode.ToCharArray()[0];
  int sheetsCellsIndex =code -'a'+1;//因為你的Index從1開始算 如果從0的話+1可以省略
  sheet.Cells[rowIdx, sheetsCellsIndex].Value = code;
 }