• CreateExcel 导出Excel


        public class CreateExcel
        {
    
            /// <summary>
            /// 用Excel组件导出Excel文件
            /// </summary>
            /// <param name="path"></param>
            /// <param name="datable"></param>
            /// <param name="msg"></param>
            /// <returns></returns>
            public static bool SaveExcel(string path,DataSet datable,ref string msg)
            {
    
                Application exlApp = new ApplicationClass();
    
                if (exlApp == null)
                {
                    msg = "Excel 未安装或无法启动!";
                    return false;
                }
    
                Workbook exlworke = exlApp.Workbooks.Add(true);
                Worksheet exlworkSheet =(Worksheet)exlworke.ActiveSheet;
    
               
                int colunindex = 0;
                int rowindex = 0;
                int colunCount = datable.Tables[0].Columns.Count;
                int rowCount = datable.Tables[0].Rows.Count;
    
                Range exlrange = exlworkSheet.get_Range(exlApp.Cells[1, 1], exlApp.Cells[1, colunCount]);
                exlrange.MergeCells = true;
                exlApp.ActiveCell.FormulaR1C1 = "操作人: " + msg + "   导出时间:" + System.DateTime.Now.ToLongDateString();
                exlApp.ActiveCell.Font.Size = 10;
                exlApp.ActiveCell.Font.Bold = true;
                exlApp.ActiveCell.HorizontalAlignment = Constants.xlCenter;
    
                object[,] obneed = new object[rowCount + 1, colunCount + 1];
    
                foreach (DataColumn dc in datable.Tables[0].Columns)
                {
                    obneed[rowindex, colunindex++] = dc.ColumnName;
                }
                rowindex++;
    
                foreach (DataRow dr in datable.Tables[0].Rows)
                {
                    for(colunindex=0;colunindex<colunCount;colunindex++)
                    {
                        obneed[rowindex, colunindex] = dr[colunindex];
                    }
                    rowindex++; 
                }
    
                exlrange = exlworkSheet.get_Range(exlApp.Cells[2, 1], exlApp.Cells[rowCount+2, colunCount]);//标题和标头+2
                exlrange.Value2 = obneed;
    
                try
                {
                    exlworke.Saved = true;
                    exlworke.SaveCopyAs(path);
                }
                catch
                {
                    msg = "保存失败!";
                }
                finally
                {
                    exlApp.Quit();
                    GC.Collect();
                }
                msg = "导出成功!";
                return true;
            }
            /// <summary>
            /// 使用数据流导出Excel文件
            /// </summary>
            /// <param name="path"></param>
            /// <param name="datable"></param>
            /// <param name="msg"></param>
            /// <returns></returns>
            public static bool SaveIOExcel(string path, DataSet datable, ref string msg)
            {
             
                int colunindex = 0;
                int colunCount = datable.Tables[0].Columns.Count;
                int rowCount = datable.Tables[0].Rows.Count;
                try
                {
                    using (FileStream flStream = new FileStream(path, FileMode.Create, FileAccess.Write))
                    {
                        using (StreamWriter sw = new StreamWriter(flStream, System.Text.Encoding.GetEncoding(-0)))
                        {
                            StringBuilder strb = new StringBuilder();
                            for (colunindex = 0; colunindex < colunCount; colunindex++)
                            {
                                strb.Append(datable.Tables[0].Columns[colunindex].ColumnName).Append("	");
                            }
                            strb.AppendLine();
                            foreach (DataRow dr in datable.Tables[0].Rows)
                            {
                                for (colunindex = 0; colunindex < colunCount; colunindex++)
                                {
                                    strb.Append(dr[colunindex]).Append("	");
                                }
                                strb.AppendLine();
                            }
                            strb.AppendLine("操作人: " + msg + "   导出时间:" + System.DateTime.Now.ToLongDateString());
                            sw.Write(strb.ToString());
                        }
    
                    }
                }
                catch
                {
                    msg = "转换失败!";
                    return false;
                }
                msg = "导出成功!";
                return true;
            }
        }
  • 相关阅读:
    鼠标滑动显示层
    软件过程+改进 +UML
    接口和抽象类的区别
    SQL SERVER – Difference Between EXEC and EXECUTE vs EXEC() – Use EXEC/EXECUTE for SP always
    XDocument.Parse Method (String)
    Robocopy use case
    Two Solutions to fix issue: ORDER BY items must appear in the select list if the statement contains a UNION, ...
    [转载]:SQL Server 连接字符串和身份验证
    Insert Data From One Table to Another
    volatile (C# Reference)
  • 原文地址:https://www.cnblogs.com/dragon-L/p/3777217.html
Copyright © 2020-2023  润新知