DataTable导出Excel
admin
2024-02-09 20:46:03

  private static Boolean ExpordExcel(DataTable dt, string filePath, string ReportTitle, bool isTitle)
        {
            writelog("ExpordExcel 导出资料" );
            if (dt == null || dt.Rows.Count <=0)
            {
                writelog("ExpordExcel 没有资料");
                return false;

            }
            int eRowIndex = 1;
            int eColIndex = 1;
            int cols = dt.Columns.Count;
            int rows = dt.Rows.Count;
            Excel.Application xlApp = new Excel.ApplicationClass();
            Excel.Workbook xlBook = xlApp.Workbooks.Add(true);


            Excel.Worksheet xlWorksheet;

            //xlWorksheet = (Excel.Worksheet)xlBook.Worksheets.Add(Type.Missing, Type.Missing, 1, Type.Missing);//增加
            xlWorksheet = (Excel.Worksheet)xlBook.Worksheets.get_Item(1);//取第一个Sheet

            xlWorksheet.Name = ReportTitle;//表的名
            

            
            try
            {
                //标题的处理
                if (isTitle)
                {
                    Excel.Range range = xlApp.get_Range(xlApp.Cells[1, 1], xlApp.Cells[1, cols]);
                    range.MergeCells = true;
                    xlApp.ActiveCell.FormulaR1C1 = ReportTitle;
                    xlApp.ActiveCell.Font.Size = 18;
                    xlApp.ActiveCell.Font.Bold = true;
                    xlApp.ActiveCell.VerticalAlignment = Excel.Constants.xlCenter;
                    xlApp.ActiveCell.HorizontalAlignment = Excel.Constants.xlCenter;
                    eRowIndex++;
                }
                //列名的处理
                for (int i = 0; i < cols; i++)
                {
                    xlApp.Cells[eRowIndex, eColIndex] = dt.Columns[i].ColumnName;
                    eColIndex++;
                }
                //列名加粗显示
                xlApp.get_Range(xlApp.Cells[eRowIndex, 1], xlApp.Cells[eRowIndex, cols]).Font.Bold = true;
                eRowIndex++;
                for (int i = 0; i < rows; i++)
                {
                    eColIndex = 1;
                    for (int j = 0; j < cols; j++)
                    {
                        xlApp.get_Range(xlApp.Cells[eRowIndex, eColIndex], xlApp.Cells[eRowIndex, eColIndex]).NumberFormatLocal = "@";//设置文本
                        xlApp.Cells[eRowIndex, eColIndex] = string.Format("{0}",dt.Rows[i][j].ToString());
                       
                        eColIndex++;
                    }
                    eRowIndex++;
                }
                //控制单元格中的内容。
                xlApp.Cells.EntireColumn.AutoFit();
                xlApp.DisplayAlerts = false;

                xlBook.SaveCopyAs(filePath);
                xlApp.Workbooks.Close();

                return true;

            }
            catch (Exception ex)
            {

                writelog(string.Format("ExpordExcel错误,{0}",ex.ToString()) ,true  );

                return false;
                //throw;

            }
            finally
            {
                xlApp.Quit();
                GC.Collect();

            }

        }
 

相关内容

热门资讯

大成中证上海环交所碳中和ETF... 数据显示,12月26日,大成中证上海环交所碳中和ETF(159642)遭净赎回104.5万元,位居当...
博拓生物涨2.22%,成交额6... 12月29日,博拓生物盘中上涨2.22%,截至09:39,报39.68元/股,成交673.73万元,...
凯瑞德2025年12月29日涨... 2025年12月29日,凯瑞德(维权)(sz002072)触及涨停,涨停价7.81元,涨幅9.97%...
中金黄金跌2.01%,成交额3... 12月29日,中金黄金盘中下跌2.01%,截至09:40,报22.97元/股,成交3.83亿元,换手...
南方中证上海环交所碳中和ETF... 数据显示,12月26日,南方中证上海环交所碳中和ETF(159639)遭净赎回840.4万元,位居当...