C# 转换数据结构表为select语句

本博客详细介绍了如何使用C#从Excel中读取数据,并将其转换为字符串数组进行进一步处理。通过实例展示了如何遍历Excel表格的每一行,获取特定列的数据,并将这些数据格式化为字符串。此外,还提供了如何处理可能遇到的空值的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

            Excel.Application app = Globals.ThisAddIn.Application;
            Excel.Worksheet sht = app.ActiveSheet as Excel.Worksheet;
            Excel.Range rng = sht.UsedRange;
            object[,] objs = rng.Value2;

            string statementSeparator = "", ele1 = "", ele2 = "", ele3 = "";
            string pre = "t.";
            List<string> ls = new List<string>();
            List<string> lsTitle = new List<string>();
            for (int i = 2; i <= objs.GetUpperBound(0); i++)
            {
                string[] obj = getStrArr(objs, i);
                statementSeparator = 2 == i ? "" : ",";
                ele1 = obj[4 - 1]; //属性
                ele2 = obj[3 - 1]; //属性名称
                ele3 = string.Format("{0}{1} {2}", obj[8 - 1], MyCommon.LetterToInt(obj[8 - 1])
                    , obj[9 - 1].Replace("\"", "'") + " " + obj[10 - 1].Replace("\"", "'"));

                ls.Add(string.Format("\t{0}{1}{2} -- [{3}]  {4}"
                        , statementSeparator, pre
                        , ele1, ele2, ele3)
                   );
                lsTitle.Add(string.Format("\"{0}\"", ele2));
            }

            MyCommon.WriteLog(string.Format("string[] title = {{{0}}}"
                , string.Join(","
                    , lsTitle.ToArray()
                    )
                ));

            MyCommon.WriteLog(
                msg: string.Join(Environment.NewLine, ls.ToArray())
                , display: true);

        private string[] getStrArr(object[,] objs, int row)
        {

            int length = objs.GetLength(1);
            /*
            Array arr =  Array.CreateInstance(typeof(String), new int[] { length }, new int[] { 1 });
            for (int i = objs.GetLowerBound(1); i <= objs.GetUpperBound(1); i++)
            {
                arr.SetValue( null == objs[row, i] ? "" : objs[row, i].ToString(),i); 
            }
            return (string[])arr;
             */
            string[] result = new string[length];
            for (int i = objs.GetLowerBound(1); i <= objs.GetUpperBound(1); i++)
                result[i - 1] = null == objs[row, i] ? "" : objs[row, i].ToString();

            return result;
        }

/*
c# System.String[*] System.String[]

*/
using Excel = Microsoft.Office.Interop.Excel;
using CpmBigDataImportService;

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值