Exemplo n.º 1
0
        /// <summary>
        /// 生成EXCEL模板文件和执行文件
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="strFilePath"></param>
        /// <param name="strKeyWord"></param>
        public void ScEXCEL(SheetPrint obj, string strFilePath, string strKeyWord, ref string error)
        {
            lock (this)
            {
                try
                {
                    if (obj == null)
                    {
                        return;
                    }
                    if (string.IsNullOrEmpty(obj.Parameter))
                    {
                        error = "设置参数不全,无法生成模板!";
                        return;
                    }
                    if (string.IsNullOrEmpty(obj.SQLText))
                    {
                        error = "设置SQL不全,无法生成模板!";
                        return;
                    }
                    if (string.IsNullOrEmpty(obj.Path))
                    {
                        error = "未指定EXCEL模板!";
                        return;
                    }
                    string templeFilePath = obj.Path;
                    if (!templeFilePath.StartsWith(@"\"))
                    {
                        templeFilePath = @"\" + templeFilePath;
                    }
                    templeFilePath = AppDomain.CurrentDomain.BaseDirectory + "ExcelTemple\\" + templeFilePath;
                    if (!File.Exists(templeFilePath))
                    {
                        error = "指定的EXCEL模板不存在!";
                        return;
                    }

                    //生成EXCEL模板文件
                    System.IO.FileStream fs = new System.IO.FileStream(templeFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite);

                    byte[] bytes = new byte[(int)fs.Length];

                    fs.Read(bytes, 0, bytes.Length);

                    fs.Close();

                    //if (!File.Exists(strFilePath)) Directory.CreateDirectory(strFilePath);
                    string strModuleExcel = strFilePath + strKeyWord + ".xls";
                    File.Delete(strModuleExcel);
                    FileStream   fs_stream = new FileStream(strModuleExcel, FileMode.CreateNew);
                    BinaryWriter writefile = new BinaryWriter(fs_stream);
                    writefile.Write(bytes);
                    writefile.Close();

                    //2.生成XLEXCEL文件
                    string       strModuleXlt = strFilePath + strKeyWord + ".xrf";
                    FileStream   fsxls        = new FileStream(strModuleXlt, FileMode.OpenOrCreate, FileAccess.Write);
                    StreamWriter sw           = new StreamWriter(fsxls, System.Text.Encoding.GetEncoding("GB2312"));//通过指定字符编码方式可以实现对汉字的支持,否则在用记事本打开查看会出现乱码
                    sw.Flush();
                    sw.BaseStream.Seek(0, SeekOrigin.Begin);

                    GetConnectString con     = new GetConnectString();
                    GetDataSouce     conn    = con.GetDataSouce();
                    string           strText = "";
                    strText = "ExcelReport Version 2.0" + "\r\n" +
                              "[Data Source]" + "\r\n" +
                              "Name1=QswmExcelExport" + "\r\n" +
                              "User1=" + "sa" + "\r\n" +
                              "Password1=" + conn.UsePassword + "\r\n" +
                              "" + "\r\n" +
                              "[File] " + "\r\n" +
                              "ReportTemplateFileName=" + strKeyWord + ".xls" + "\r\n" +
                              "ReportFileName=Report\\" + strKeyWord + ".xls" + "\r\n" +
                              "LogFileName=" + strFilePath + strKeyWord + ".log" + "\r\n"
                    ;

                    strText = strText + obj.Parameter + "\r\n" + "[SQL]" + "\r\n" + obj.SQLText;
                    sw.WriteLine(strText);
                    sw.Flush();
                    sw.Close();
                }
                catch (Exception ex)
                {
                    error = "生成Excel失败!" + ex.Message;
                    //string strModuleExcel = strFilePath + "Errorxls.txt";

                    //FileStream fs_stream = new FileStream(strModuleExcel, FileMode.CreateNew);
                    //BinaryWriter writefile = new BinaryWriter(fs_stream);
                    //writefile.Write(HttpUtility.UrlEncode(error, System.Text.Encoding.UTF8));
                    //writefile.Close();

                    throw ex;
                }
            }
        }
Exemplo n.º 2
0
        public void ShowPrintView(string strReportName, string tt_no, List <string> parms, ref string strFileName, ref string error)
        {
            lock (this)
            {
                try
                {
                    strFileName = "";


                    string strFilePath = AppDomain.CurrentDomain.BaseDirectory;
                    if (strFilePath.EndsWith("\\"))
                    {
                        strFilePath = strFilePath + "EXCEL\\";
                    }
                    else
                    {
                        strFilePath = strFilePath + "\\EXCEL\\";
                    }

                    #region 判断文件夹,如果不存在则重新创建
                    //if (Directory.Exists(strFilePath))
                    //{
                    //    try
                    //    {
                    //        Directory.Delete(strFilePath, true);
                    //    }
                    //    catch
                    //    {
                    //        error = "生成报表文件时删除原报表格式错误,可能是EXCEL 文件夹下文件正在使用,请先关闭!";
                    //        return;
                    //    }
                    //}

                    //Directory.CreateDirectory(strFilePath);
                    if (!Directory.Exists(strFilePath))
                    {
                        Directory.CreateDirectory(strFilePath);
                    }

                    #endregion
                    //ChangRegedit(ref error);//注册数据源
                    SheetPrint print1 = new SheetPrint();
                    //测试用
                    //获取导出模板
                    string[] paras = null;
                    PubMethod.GetExcelParm(strReportName, tt_no, ref paras);
                    if (paras == null)
                    {
                        return;
                    }
                    else
                    {
                        print1.Parameter = paras[0];
                        print1.SQLText   = paras[1];
                        print1.Path      = paras[2];
                    }
                    strFileName = Guid.NewGuid().ToString();
                    ScEXCEL(print1, strFilePath, strFileName, ref error);
                    if (string.IsNullOrEmpty(error))
                    {
                        ScBb(parms, strFilePath, strFileName, ref error);//生成报表
                        if (!string.IsNullOrEmpty(error))
                        {
                            strFileName = "";
                        }
                    }
                    else
                    {
                        strFileName = "";
                    }

                    GC.Collect();
                }
                catch (Exception ex)
                {
                    if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "Report\\" + strFileName + ".xls"))
                    {
                        File.Delete(AppDomain.CurrentDomain.BaseDirectory + "Report\\" + strFileName + ".xls");
                    }
                    strFileName = "";
                    throw ex;
                }
                finally
                {
                    GC.Collect();
                    //GC.WaitForPendingFinalizers();
                }
            }
        }