Exemple #1
0
        void Export(bool p_IsTemplate)
        {
            try
            {
                DataTable dt = AssetInfo.getExAssetList();

                DataTable dtTemp = dt.Copy();

                if (dt.Rows.Count > 0)
                {

                    //��ȡ·��
                    string strPath = AppDomain.CurrentDomain.BaseDirectory.Substring(0, AppDomain.CurrentDomain.BaseDirectory.Length - 1);
                    strPath = strPath.Replace("/", "\\");
                    DateTime time1 = DateTime.Now;
                    string temlpate = strPath + "\\template\\������������.xls";
                    strPath = strPath + "\\excel";
                    List<ExcelColumInfo> ExcelColumns = new List<ExcelColumInfo>();
                    foreach (DataColumn dc in dt.Columns)
                    {
                        ExcelColumInfo colinfo = new ExcelColumInfo();
                        colinfo.HeadName = dc.ColumnName;
                        colinfo.TableColumnName = dc.ColumnName;
                        colinfo.Width = 20;
                        colinfo.DataType = DataType.Text;
                        if (dc.ColumnName == "MCDate" || dc.ColumnName == "WriteDate")
                            colinfo.DataType = DataType.DateTime;//ShareddedRate
                        else if (dc.ColumnName == "Processes" || dc.ColumnName == "Reject" || dc.ColumnName == "RejectRate" || dc.ColumnName == "Sharedded" || dc.ColumnName == "ShareddedRate" || dc.ColumnName == "RejShareddedRateect" || dc.ColumnName == "RunHour")
                            colinfo.DataType = DataType.Number;
                        ExcelColumns.Add(colinfo);
                    }

                    ExcelHelp eh = new ExcelHelp();
                    byte[] byteResult;
                    //�������ݵ�byte�ֽ�����
                    if (!p_IsTemplate)
                        byteResult = eh.ExportData(dt, ExcelColumns, "�칫��Ʒ����", strPath, 5000);
                    else
                        byteResult = eh.ExportData(dt, ExcelColumns, "�칫��Ʒ����", strPath, 5000, temlpate);

                    DateTime time2 = DateTime.Now;
                    TimeSpan ts1 = new TimeSpan(time1.Ticks);
                    TimeSpan ts2 = new TimeSpan(time2.Ticks);
                    TimeSpan ts = ts1.Subtract(ts2).Duration();
                    string dateDiff = "����1�����ݱ����" + dt.Rows.Count.ToString() + "����¼����ʱ" + ts.Days.ToString() + "��"
                    + ts.Hours.ToString() + "Сʱ"
                    + ts.Minutes.ToString() + "����"
                    + ts.Seconds.ToString() + "��";
                    //lblOledb.Text = dateDiff;
                    //�ͻ��������ļ�
                    Response.Clear();
                    //Response.Buffer = true;
                    Response.Charset = "GB2312";
                    Response.ContentEncoding = System.Text.Encoding.UTF8;
                    //  ���ͷ��Ϣ��Ϊ"�ļ�����/���Ϊ"�Ի���ָ��Ĭ���ļ���
                    Response.AddHeader("Content-Disposition", "attachment; filename = " + Server.UrlEncode("�칫��Ʒ����.xls"));
                    //  ���ͷ��Ϣ��ָ���ļ���С����������ܹ���ʾ���ؽ���
                    Response.AddHeader("Content-Length", byteResult.Length.ToString());
                    //  ָ�����ص���һ�����ܱ��ͻ��˶�ȡ���������뱻����
                    Response.ContentType = "application/ms-excel";
                    //  ���ļ������͵��ͻ���
                    //Response.WriteFile(file.FullName);
                    Response.BinaryWrite(byteResult);
                    Response.Flush();
                    //  ֹͣҳ���ִ��
                    //GC.Collect();
                    //Response.End();
                    HttpContext.Current.ApplicationInstance.CompleteRequest();
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('����Ϊ�յ���ʧ�ܣ�');</script>");
                }
            }
            catch (Exception Ex)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "Save", "alert('��������ʧ�ܣ�" + Ex.Message + "');", true);

            }
        }
Exemple #2
0
        void Export(bool p_IsTemplate)
        {
            try
            {
                DataTable dt = AssetInfo.getExAssetList();

                DataTable dtTemp = dt.Copy();


                if (dt.Rows.Count > 0)
                {
                    //获取路径
                    string strPath = AppDomain.CurrentDomain.BaseDirectory.Substring(0, AppDomain.CurrentDomain.BaseDirectory.Length - 1);
                    strPath = strPath.Replace("/", "\\");
                    DateTime time1    = DateTime.Now;
                    string   temlpate = strPath + "\\template\\机器性能数据.xls";
                    strPath = strPath + "\\excel";
                    List <ExcelColumInfo> ExcelColumns = new List <ExcelColumInfo>();
                    foreach (DataColumn dc in dt.Columns)
                    {
                        ExcelColumInfo colinfo = new ExcelColumInfo();
                        colinfo.HeadName        = dc.ColumnName;
                        colinfo.TableColumnName = dc.ColumnName;
                        colinfo.Width           = 20;
                        colinfo.DataType        = DataType.Text;
                        if (dc.ColumnName == "MCDate" || dc.ColumnName == "WriteDate")
                        {
                            colinfo.DataType = DataType.DateTime;//ShareddedRate
                        }
                        else if (dc.ColumnName == "Processes" || dc.ColumnName == "Reject" || dc.ColumnName == "RejectRate" || dc.ColumnName == "Sharedded" || dc.ColumnName == "ShareddedRate" || dc.ColumnName == "RejShareddedRateect" || dc.ColumnName == "RunHour")
                        {
                            colinfo.DataType = DataType.Number;
                        }
                        ExcelColumns.Add(colinfo);
                    }

                    ExcelHelp eh = new ExcelHelp();
                    byte[]    byteResult;
                    //导出数据到byte字节数组
                    if (!p_IsTemplate)
                    {
                        byteResult = eh.ExportData(dt, ExcelColumns, "办公用品数据", strPath, 5000);
                    }
                    else
                    {
                        byteResult = eh.ExportData(dt, ExcelColumns, "办公用品数据", strPath, 5000, temlpate);
                    }


                    DateTime time2    = DateTime.Now;
                    TimeSpan ts1      = new TimeSpan(time1.Ticks);
                    TimeSpan ts2      = new TimeSpan(time2.Ticks);
                    TimeSpan ts       = ts1.Subtract(ts2).Duration();
                    string   dateDiff = "导出1个数据表,共" + dt.Rows.Count.ToString() + "条记录,耗时" + ts.Days.ToString() + "天"
                                        + ts.Hours.ToString() + "小时"
                                        + ts.Minutes.ToString() + "分钟"
                                        + ts.Seconds.ToString() + "秒";
                    //lblOledb.Text = dateDiff;
                    //客户端下载文件
                    Response.Clear();
                    //Response.Buffer = true;
                    Response.Charset         = "GB2312";
                    Response.ContentEncoding = System.Text.Encoding.UTF8;
                    //  添加头信息,为"文件下载/另存为"对话框指定默认文件名
                    Response.AddHeader("Content-Disposition", "attachment; filename = " + Server.UrlEncode("办公用品数据.xls"));
                    //  添加头信息,指定文件大小,让浏览器能够显示下载进度
                    Response.AddHeader("Content-Length", byteResult.Length.ToString());
                    //  指定返回的是一个不能被客户端读取的流,必须被下载
                    Response.ContentType = "application/ms-excel";
                    //  把文件流发送到客户端
                    //Response.WriteFile(file.FullName);
                    Response.BinaryWrite(byteResult);
                    Response.Flush();
                    //  停止页面的执行
                    //GC.Collect();
                    //Response.End();
                    HttpContext.Current.ApplicationInstance.CompleteRequest();
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('数据为空导出失败!');</script>");
                }
            }
            catch (Exception Ex)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "Save", "alert('导出数据失败:" + Ex.Message + "');", true);
            }
        }