zoukankan      html  css  js  c++  java
  • 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;
            }
        }
  • 相关阅读:
    【Lintcode】91 最小调整代价
    【LintCode】29 交叉字符串
    十二、动态规划
    HttpClient的简单使用
    ajax跨域请求
    session共享
    八大排序算法
    MAC 脚本批量启动应用
    知识点整理-bio、nio的简单demo
    知识点整理-数组如何实现随机访问?
  • 原文地址:https://www.cnblogs.com/dragon-L/p/3777217.html
Copyright © 2011-2022 走看看