C#写excel

    /// <summary>
        /// 将取得的结果写入excel
        /// </summary>
        /// <param name="result">结果</param>
        /// <param name="fileName">文件名称</param>
        /// <param name="rowNum">行数</param>
        private void writeToExcel(List<Dictionary<string, DataTable>> result, string fileName, int totalNum)
        {
            //创建Excel对象
            Excel.Application excelApp = new Excel.ApplicationClass();

            //新建工作簿
            Excel.Workbook workBook = excelApp.Workbooks.Add(true);

            List<string> newQuName = getLocation();

            //记录每个区的最大行数(即每个区中查询的结果表中记录数最多的)
            int[] maxRowsNum = new int[newQuName.Count];

            //创建工作表
            for (int i = 0; i < newQuName.Count - 1; i++)
            {
                excelApp.Sheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                maxRowsNum[i] = 0;
            }
            bool isNewLine = false;


            //更改名称
            //循环更改每个区的excel文件
            for (int i = 1; i <= newQuName.Count; i++)
            {
                //找到该区对应的工作表
                Excel.Worksheet workSheet = (Excel.Worksheet)excelApp.Sheets[i];
                string name = newQuName[i - 1];
                workSheet.Name = name;

                int colNum = 1;
                List<string> headTitle = new List<string>();

                for (int k = 0; k < result.Count; k++)
                {
                    Dictionary<string, DataTable> tempDic = result[k];
                    //没有数据,则跳过
                    if (tempDic.Count == 0)
                    {
                        continue;
                    }
                    isNewLine = false;
                    if (tempDic.ContainsKey(name))
                    {
                        DataTable table = tempDic[name];
                        for (int x = 0; x < table.Columns.Count; x++)
                        {
                            string colName = table.Columns[x].ColumnName;
                            headTitle.Add(colName);
                            colNum++;
                            isNewLine = true;
                        }
                        //记录每个区的最大行数(即每个区中查询的结果表中记录数最多的)
                        if (table.Rows.Count > maxRowsNum[i - 1])
                        {
                            maxRowsNum[i - 1] = table.Rows.Count;
                        }
                    }
                    if (isNewLine)
                    {
                        headTitle.Add("");
                        colNum++;
                    }
                }
                //一次性写入
                string beginNum = "A1";
                char endChar = (char)(65 + colNum - 2);
                string endNum = endChar + "1";
                Excel.Range rowRange = workSheet.get_Range(beginNum, endNum);

                string[] rowValuesArr = headTitle.ToArray();
                rowRange.Value2 = rowValuesArr;

                //自动列宽
                workSheet.Cells.Columns.AutoFit();
                //设置居中
                workSheet.Cells.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
            }

            //循环读取每个区的信息
            for (int g = 0; g < newQuName.Count; g++)
            {
                Excel.Worksheet workSheet = (Excel.Worksheet)excelApp.Sheets[g + 1];
                string name = newQuName[g];

                //读取每行信息,每行都是从A开始,到 A+该行个数
                //第一行从A2开始,接下来A3,依次递增
                //行  A+ (rowNums +2 )  列    (CHAR(A+rowNums))+(rowNums +2 )
                int rowNums = 0;
                //每行列数
                int colNum = 1;

                while (rowNums <= maxRowsNum[g])
                {
                    colNum = 1;
                    List<string> rowValues = new List<string>();
                    for (int i = 0; i < result.Count; i++)
                    {
                        isNewLine = false;
                        if (result[i].Count > 0)
                        {
                            Dictionary<string, DataTable> tempDic = result[i];
                            if (tempDic.ContainsKey(name))
                            {
                                DataTable table = tempDic[name];
                                DataRow row = null;
                                try
                                {
                                    row = table.Rows[rowNums];
                                    for (int x = 0; x < table.Columns.Count; x++)
                                    {
                                        string content = row[x].ToString();
                                        rowValues.Add(content);
                                        colNum++;
                                        isNewLine = true;
                                    }
                                }
                                catch (IndexOutOfRangeException)
                                {
                                    for (int b = 0; b < table.Columns.Count; b++)
                                    {
                                        rowValues.Add("");
                                        colNum++;
                                        isNewLine = true;
                                    }
                                }
                            }
                        }
                        if (isNewLine)
                        {
                            rowValues.Add("");
                            colNum++;
                        }
                    }
                    //一次性写入一行,数据写入excel速度较快
                    string beginNum = "A" + (rowNums + 2);
                    char endChar = (char)(65 + colNum - 2);
                    string endNum = endChar + "" + (rowNums + 2);
                    Excel.Range rowRange = workSheet.get_Range(beginNum, endNum);

                    string[] rowValuesArr = rowValues.ToArray();
                    rowRange.Value2 = rowValuesArr;
                    rowNums++;
                }
                //自动列宽
                workSheet.Cells.Columns.AutoFit();
                //设置居中
                workSheet.Cells.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
            }


            //保存文件
            workBook.SaveCopyAs(fileName);
            workBook.Saved = true;
            workBook.Close(false, fileName, false);
            //excelApp.Quit();
            QuitExcel(ref excelApp);
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值