Excel c# 操作

1.直接导出数据控件保存方法

 

 private void ExportToExcel()
    {
        if(Session["OBData"] != null && Session["OBTotal"] != null)
        {
            DataTable dt = (DataTable)Session["OBData"]; // datatable
            DataTable dt_total = (DataTable)Session["OBTotal"]; // total table
            // Populate the repeater control with the Items DataSet
            if (dt_total != null && dt_total.Rows.Count > 0)
            {
                this.TotalCompanyQtyOnhand = string.Format("{0:N0}", dt_total.Rows[0]["TotalCompanyQtyOnhand"]);
                this.TotalCompanyExt = string.Format("{0:N}", dt_total.Rows[0]["TotalCompanyExt"]);
                this.TotalLCMReserve = string.Format("{0:N}", dt_total.Rows[0]["TotalLCMReserve"]);
                this.TotalEFCQty = string.Format("{0:N0}", dt_total.Rows[0]["TotalEFCQty"]);
                this.TotalCFCQty = string.Format("{0:N0}", dt_total.Rows[0]["TotalCFCQty"]);
                this.TotalWFCQty = string.Format("{0:N0}", dt_total.Rows[0]["TotalWFCQty"]);
                this.TotalEFCExt = string.Format("{0:N}", dt_total.Rows[0]["TotalEFCExt"]);
                this.TotalCFCExt = string.Format("{0:N}", dt_total.Rows[0]["TotalCFCExt"]);
                this.TotalWFCExt = string.Format("{0:N}", dt_total.Rows[0]["TotalWFCExt"]);
                this.TotalStoreExt = string.Format("{0:N}", dt_total.Rows[0]["TotalStoreExt"]);
                this.TotalStoreQty = string.Format("{0:N0}", dt_total.Rows[0]["TotalStoreQty"]);
            }

            if (dt != null && dt.Rows.Count > 0)
            {
                this.ReportRepeater.DataSource = dt;
                this.ReportRepeater.DataBind();
            }
        }
        if (Session["OBSummary"] != null)
        {
            this.BindSummaryRow((DataTable)Session["OBSummary"]);
        }
        string attachment = "attachment; filename=InventoryObsSkuReport_" + DateTime.Now.ToShortDateString().Replace("/", "") + ".xls";
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/ms-excel";
        StringWriter stringWriter = new StringWriter();
        HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
        this.SummaryRow.RenderControl(htmlTextWriter);
        this.ReportRepeater.RenderControl(htmlTextWriter);
        Response.Write(stringWriter.ToString());
        Response.End();
    }

这种SIZE比较大,数据大时会内存会溢出。

2.读取数据拼凑方法再抛出保存

 

 private void ExportToExcel()
    {
        string sql = "exec Report_Inventory @Date='" + Request.Params["date"] + "',@sku='" + Request.Params["sku"] + "',@vendornumber='" + Request.Params["vendornumber"] + "',@WHView='R',@PageNum='-1',@categoryid='" + Request.Params["categoryid"] + "'";
        DataSet ds = Suryani.Util.DBService.GetDataSet(sql);
        string sqlR = "exec GetNotInReceivings @date='" + Request.Params["date"] + "',@sku='" + Request.Params["sku"] + "',@vendornumber='" + Request.Params["vendornumber"] + "',@WHView='R',@categoryid='" + Request.Params["categoryid"] + "'";
        DataSet ds_notIn = Suryani.Util.DBService.GetDataSet(sqlR);
        StringBuilder sb = new StringBuilder();
        if (ds != null && ds.Tables.Count == 4)
        {
            DataTable dt_wh = ds.Tables[0];
            DataTable dt_Sku = ds.Tables[1];
            DataTable dt_data = ds.Tables[2];
            DataTable dt_sum = ds.Tables[3];
            DataTable dtnot_s = null;
            DataTable dtnot_d = null;

            if (dt_wh != null && dt_wh.Rows.Count > 0)
            {
                // table header
                sb.Append("<table cellspacing=\"0\" rules=\"all\" bordercolor=\"Gray\" border=\"1\"  style=\"background-color:White;border-color:Gray;width:100%;border-collapse:collapse;\">");
                sb.Append("<tr class=\"ShoppingCartHeader\">");
                sb.Append("<td colspan=\"4\"></td>");
                sb.Append("<td width=\"2\" bgcolor=\"Gray\"></td>");
                foreach (DataRow drw in dt_wh.Rows)
                {
                    string key = drw["warehousecode"].ToString().Trim();
                    sb.Append("<td align=\"center\" colspan=\"3\"><b>" + key + "</b></td>");
                    sb.Append("<td width=\"2\" bgcolor=\"Gray\"></td>");
                }
                sb.Append("<td align=\"center\" colspan=\"3\"><b>Total</b></td>");
                sb.Append("</tr>");

                // header2
                sb.Append("<tr class=\"ShoppingCartHeader\">");
                sb.Append("<td>SKU</td>");
                sb.Append("<td colspan=\"\">Description</td>");
                sb.Append("<td style=\"white-space:nowrap\"><b>Related Skus</b></td>");
                sb.Append("<td><b>Category</b></td>");
                sb.Append("<td width=\"2\" bgcolor=\"Gray\"></td>");

                foreach (DataRow drw in dt_wh.Rows)
                {
                    string key = drw["warehousecode"].ToString().Trim();
                    sb.Append("<td align=\"center\"><b>Qty</b></td><td align=\"center\"><b>Cost</b></td><td align=\"center\"><b>Extended</b></td>");
                    sb.Append("<td width=\"2\" bgcolor=\"Gray\"></td>");
                }
                sb.Append("<td align=\"center\"><b>Qty</b></td><td align=\"center\"><b>Cost</b></td><td align=\"center\"><b>Extended</b></td>");
                sb.Append("</tr>");

                DataRow[] drs_D;
                // data part
                if (dt_Sku != null && dt_Sku.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt_Sku.Rows)
                    {
                        sb.Append("<tr>");
                        sb.Append("<td style=\"white-space:nowrap\"><a href=\"" + GetSkuLink2(dr["sku"].ToString()) + "\">" + dr["sku"].ToString() + "</a></td>");
                        sb.Append("<td style=\"white-space:nowrap\">" + dr["des"].ToString() + "</td>");
                        sb.Append("<td style=\"white-space:nowrap\">" + dr["relatedskus"].ToString() + "</td>");
                        sb.Append("<td style=\"white-space:nowrap\">" + dr["categoryname"].ToString() + "</td>");
                        sb.Append("<td width=\"2\" bgcolor=\"Gray\"></td>");

                        foreach (DataRow drw in dt_wh.Rows)
                        {
                            string key = drw["warehousecode"].ToString().Trim();

                            drs_D = dt_data.Select("warehousecode='" + key + "' and sku='" + dr["sku"].ToString() + "'");
                            if (drs_D != null && drs_D.Length > 0)
                            {
                                sb.Append("<td align=\"center\">" + string.Format("{0:N0}", drs_D[0]["Qty"]) + "</td><td align=\"center\"><a href=\"" + GetCostLink2(dr["sku"].ToString(), " + key + ", dr["basesku"].ToString()) + "\">" + string.Format("{0:N2}", drs_D[0]["Cost"]) + "</a></td><td align=\"center\">" + string.Format("{0:N2}", drs_D[0]["extended"]) + "</td>");
                            }
                            else
                            {
                                sb.Append("<td align=\"center\"></td><td align=\"center\"></td><td align=\"center\"></td>");
                            }
                            sb.Append("<td width=\"2\" bgcolor=\"Gray\"></td>");
                        }

                        sb.Append("<td align=\"center\">" + string.Format("{0:N0}", dr["TotalQty"]) + "</td><td align=\"center\">" + string.Format("{0:N2}", dr["TotalCost"]) + "</td><td align=\"right\">" + string.Format("{0:N2}", dr["Totalext"]) + "</td>");
                        sb.Append("</tr>");
                    }
                }

                // sub total row
                int totalqty = 0, tQty = 0;
                double totalExt = 0, tExt = 0;
                if (dt_sum != null && dt_sum.Rows.Count > 0)
                {
                    sb.Append("<tr>");
                    sb.Append("<td><b>Subtotal:</b></td><td colspan=\"3\"></td>");
                    sb.Append("<td width=\"2\" bgcolor=\"Gray\"></td>");
                    foreach (DataRow drw in dt_wh.Rows)
                    {
                        string key = drw["warehousecode"].ToString().Trim();
                        drs_D = dt_sum.Select("warehousecode='" + key + "'");
                        if (drs_D != null && drs_D.Length > 0)
                        {
                            sb.Append("<td align=\"center\">" + string.Format("{0:N0}", drs_D[0]["TotalQty"]) + "</td><td align=\"center\"></td><td align=\"center\">" + string.Format("{0:N2}", drs_D[0]["Totalextended"]) + "</td>");
                            try
                            {
                                tQty = int.Parse(drs_D[0]["TotalQty"].ToString());
                                tExt = double.Parse(drs_D[0]["Totalextended"].ToString());
                                totalqty = totalqty + tQty;
                                totalExt = totalExt + tExt;
                            }
                            catch { }
                        }
                        else
                        {
                            sb.Append("<td align=\"center\"></td><td align=\"center\"></td><td align=\"center\"></td>");
                        }

                        sb.Append("<td width=\"2\" bgcolor=\"Gray\"></td>");

                    }
                    sb.Append("<td align=\"center\">" + string.Format("{0:N0}", totalqty) + "</td><td align=\"center\"></td><td align=\"right\">" + string.Format("{0:N2}", totalExt) + "</td>");
                    sb.Append("</tr>");
                }

                int whNum = dt_wh.Rows.Count;
                int ColSpanNum = 14 + whNum * 3;
                sb.Append("<tr><td colspan=\"" + ColSpanNum.ToString() + "\">&nbsp;</td></tr>");
                sb.Append("<tr><td colspan=\"2\"><b>Trucks Arrived but not Received In:</b></td>");
                sb.Append("<td align=\"center\"><b>Truck ARR Date</b></td>");
                sb.Append("<td align=\"center\"><b>Receive Date</b></td>");
                sb.Append("<td width=\"2\" bgcolor=\"Gray\"></td>");
                int tempNum = ColSpanNum - 5;
                sb.Append("<td colspan=\"" + tempNum.ToString() + "\"></tr>");

                // not in
                if (ds_notIn != null && ds_notIn.Tables.Count == 2)
                {
                    dtnot_d = ds_notIn.Tables[0]; // data
                    dtnot_s = ds_notIn.Tables[1]; // subtotal
                    // data
                    if (dtnot_d != null && dtnot_d.Rows.Count > 0)
                    {
                        foreach (DataRow dr in dtnot_d.Rows)
                        {
                            double ext = 0;
                            sb.Append("<tr>");
                            sb.Append("<td style=\"white-space:nowrap\"><a href=\"" + GetReceiveLink(dr["receivedid"].ToString().Trim(), dr["po_id"].ToString().Trim(), dr["ReceiveNumber"].ToString().Trim()) + "\" Target=\"_blank\">" + dr["ReceiveNumber"].ToString() + "</a></td>");
                            sb.Append("<td style=\"white-space:nowrap\">" + dr["vendorname"].ToString() + "</td>");
                            sb.Append("<td style=\"white-space:nowrap\" align=\"right\">" + string.Format("{0:d}", dr["TruckArriveDate"]) + "</td>");
                            sb.Append("<td style=\"white-space:nowrap\" align=\"right\">" + string.Format("{0:d}", dr["ReceiveDate"]) + "</td>");
                            sb.Append("<td width=\"2\" bgcolor=\"Gray\"></td>");

                            foreach (DataRow drw in dt_wh.Rows)
                            {
                                string key = drw["warehousecode"].ToString().Trim();
                                if (dr["warehousecode"].ToString().Trim().ToUpper() == key.ToUpper().Trim())
                                {

                                    sb.Append("<td colspan=\"3\" align=\"right\">" + string.Format("{0:N2}", dr["InvoiceAmount"]) + "</td>");
                                    try { ext = double.Parse(dr["InvoiceAmount"].ToString()); }
                                    catch { }
                                }
                                else
                                {
                                    sb.Append("<td colspan=\"3\">&nbsp;</td>");
                                }
                                sb.Append("<td width=\"2\" bgcolor=\"Gray\"></td>");
                            }

                            sb.Append("<td colspan=\"3\" align=\"right\">" + string.Format("{0:N2}", ext) + "</td>");
                            sb.Append("</tr>");
                        }
                    }

                    // summary row in Not in
                    if (dtnot_s != null && dtnot_s.Rows.Count > 0)
                    {
                        tExt = 0;
                        totalExt = 0;
                        sb.Append("<tr>");
                        sb.Append("<td><b>Subtotal:</b></td><td colspan=\"3\"><b></b></td>");
                        sb.Append("<td width=\"2\" bgcolor=\"Gray\"></td>");
                        foreach (DataRow drw in dt_wh.Rows)
                        {
                            string key = drw["warehousecode"].ToString().Trim();
                            drs_D = dtnot_s.Select("warehousecode='" + key + "'");
                            if (drs_D != null && drs_D.Length > 0)
                            {
                                sb.Append("<td align=\"right\" colspan=\"3\">" + string.Format("{0:N2}", drs_D[0]["Totalext"]) + "</td>");
                                try
                                {
                                    tExt = double.Parse(drs_D[0]["Totalext"].ToString());
                                    totalExt = totalExt + tExt;
                                }
                                catch { }
                            }
                            else
                            {
                                sb.Append("<td align=\"center\" colspan=\"3\"></td>");
                            }

                            sb.Append("<td width=\"2\" bgcolor=\"Gray\"></td>");
                        }
                        sb.Append("<td align=\"right\" colspan=\"3\">" + string.Format("{0:N2}", totalExt) + "</td>");
                        sb.Append("</tr>");
                    }
                    sb.Append("<tr><td colspan=\"" + ColSpanNum.ToString() + "\">&nbsp;</td></tr>");
                }

                // Grand TotalInvetory
                double totalLocExt = 0, totalSum = 0;
                totalExt = 0;
                sb.Append("<tr>");
                sb.Append("<td colspan=\"4\"><b>Total Inventory:</b></td>");
                sb.Append("<td width=\"2\" bgcolor=\"Gray\"></td>");
                foreach (DataRow drw in dt_wh.Rows)
                {
                    string key = drw["warehousecode"].ToString().Trim();
                    totalLocExt = 0; totalSum = 0;

                    if (dtnot_s != null && dtnot_s.Rows.Count > 0)
                    {
                        drs_D = dtnot_s.Select("warehousecode='" + key + "'");
                        if (drs_D != null && drs_D.Length > 0)
                        {
                            try
                            {
                                totalLocExt = double.Parse(drs_D[0]["Totalext"].ToString());
                            }
                            catch
                            {
                                totalLocExt = 0;
                            }
                            totalExt = totalExt + totalLocExt;
                        }
                    }
                    if (dt_sum != null && dt_sum.Rows.Count > 0)
                    {
                        drs_D = dt_sum.Select("warehousecode='" + key + "'");
                        if (drs_D != null && drs_D.Length > 0)
                        {
                            try
                            {
                                totalSum = double.Parse(drs_D[0]["Totalextended"].ToString());
                            }
                            catch
                            {
                                totalSum = 0;
                            }
                            totalExt = totalExt + totalSum;
                        }
                    }
                    sb.Append("<td align=\"right\" colspan=\"3\">" + string.Format("{0:N2}", totalLocExt + totalSum) + "</td>");
                    sb.Append("<td width=\"2\" bgcolor=\"Gray\"></td>");
                }

                sb.Append("<td align=\"right\" colspan=\"3\"><b>" + string.Format("{0:N2}", totalExt) + "</b></td>");
                sb.Append("</tr></table>");

                //this.StrSpan.InnerHtml = sb.ToString();

            }
        }
       
        string attachment = "attachment; filename=InventoryReport_" + DateTime.Now.ToShortDateString().Replace("/", "") + ".xls";
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        Response.Write(sb.ToString());
        Response.End();
    }
    private void ExportToExcel3()
    {

        string sql = "exec Report_Inventory @Date='" + Request.Params["date"] + "',@sku='" + Request.Params["sku"] + "',@vendornumber='" + Request.Params["vendornumber"] + "',@WHView='R',@PageNum='-1',@categoryid='" + Request.Params["categoryid"] + "',@CataLogId='" + Request.Params["CataLogId"] + "'";
        DataSet ds = Suryani.Util.DBService.GetDataSet(sql);
        string sqlR = "exec GetNotInReceivings @date='" + Request.Params["date"] + "',@sku='" + Request.Params["sku"] + "',@vendornumber='" + Request.Params["vendornumber"] + "',@WHView='R',@categoryid='" + Request.Params["categoryid"] + "',@CataLogId='" + Request.Params["CataLogId"] + "'";
        DataSet ds_notIn = Suryani.Util.DBService.GetDataSet(sqlR);
        if (ds != null && ds.Tables.Count == 4)
        {
            StringBuilder sbhearder = new StringBuilder();
            System_Manager System_Manager = new System_Manager();
            string _path = System_Manager.GetSystemConfiguration("InventoryReportExportPath");//"\\\\Fs1\\prod-storage\\apps\\InventoryReport\\";
            string filename = "InventoryReportSKU_RegionView_" + DateTime.Now.Date.ToShortDateString().Replace("/", "") + "_" + DateTime.Now.Millisecond.ToString() + "_" + User.Identity.Name.Replace(",", "") + ".CSV";
            System.IO.StreamWriter tw = new System.IO.StreamWriter(System.IO.File.Open(_path + filename, System.IO.FileMode.CreateNew, System.IO.FileAccess.Write));
            DataTable dt_wh = ds.Tables[0];
            DataTable dt_Sku = ds.Tables[1];
            DataTable dt_data = ds.Tables[2];
            DataTable dt_sum = ds.Tables[3];
            DataTable dtnot_s = null;
            DataTable dtnot_d = null;

            if (dt_wh != null && dt_wh.Rows.Count > 0)
            {
                // table header
                sbhearder.Append(",,,,,");
                sbhearder.Append(",");
                foreach (DataRow drw in dt_wh.Rows)
                {
                    string key = drw["warehousecode"].ToString().Trim();
                    sbhearder.Append("," + key + ",,");
                    sbhearder.Append(",");
                }
                sbhearder.Append(",Total,,");
                sbhearder.Append("\r\n");

                // header2
                sbhearder.Append("SKU,");
                sbhearder.Append("Description,");
                sbhearder.Append("Related Skus,");
                sbhearder.Append("Category,");
                sbhearder.Append("Catalog,");
                sbhearder.Append(",");

                foreach (DataRow drw in dt_wh.Rows)
                {
                    string key = drw["warehousecode"].ToString().Trim();
                    sbhearder.Append("Qty,Cost,Extended,");
                    sbhearder.Append(",");
                }
                sbhearder.Append("Qty,Cost,Extended,");
                sbhearder.Append("\r\n");
                tw.Write(sbhearder.ToString());

                DataRow[] drs_D;
                // data part
                if (dt_Sku != null && dt_Sku.Rows.Count > 0)
                {
                    StringBuilder sbskudata = new StringBuilder();
                    int skuindex = 0;
                    int index=0;
                    int skucount=dt_Sku.Rows.Count;
                    foreach (DataRow dr in dt_Sku.Rows)
                    {
                        sbskudata.Append(dr["sku"].ToString() + ",");
                        sbskudata.Append(dr["des"].ToString().Replace(",", " ") + ",");
                        sbskudata.Append(dr["relatedskus"].ToString().Replace(",", " ") + ",");
                        sbskudata.Append(dr["categoryname"].ToString().Replace(",", " ") + ",");
                        sbskudata.Append(dr["catalogname"].ToString().Replace(",", " ") + ",");
                        sbskudata.Append(",");

                        foreach (DataRow drw in dt_wh.Rows)
                        {
                            string key = drw["warehousecode"].ToString().Trim();

                            drs_D = dt_data.Select("warehousecode='" + key + "' and sku='" + dr["sku"].ToString() + "'");
                            if (drs_D != null && drs_D.Length > 0)
                            {
                                sbskudata.Append(drs_D[0]["Qty"].ToString() + "," + string.Format("{0:N2}", drs_D[0]["Cost"]).Replace(",", "") + "," + string.Format("{0:N2}", drs_D[0]["extended"]).Replace(",", "") + ",");
                            }
                            else
                            {
                                sbskudata.Append(",,,");
                            }
                            sbskudata.Append(",");
                        }

                        sbskudata.Append(dr["TotalQty"].ToString() + "," + string.Format("{0:N2}", dr["TotalCost"]).Replace(",", "") + "," + string.Format("{0:N2}", dr["Totalext"]).Replace(",", "") + ",");
                        sbskudata.Append("\r\n");
                        skuindex = skuindex + 1;
                        index=index+1;
                        if (skuindex == 1000 || index == skucount)
                        {
                            tw.Write(sbskudata.ToString());
                            skuindex = 0;
                            sbskudata = new StringBuilder();
                               
                        }
                    }
                }

                // sub total row
                StringBuilder sbskusubtotal = new StringBuilder();
                int totalqty = 0, tQty = 0;
                double totalExt = 0, tExt = 0;
                if (dt_sum != null && dt_sum.Rows.Count > 0)
                {
                    sbskusubtotal.Append("Subtotal:,,,,,");
                    sbskusubtotal.Append(",");
                    foreach (DataRow drw in dt_wh.Rows)
                    {
                        string key = drw["warehousecode"].ToString().Trim();
                        drs_D = dt_sum.Select("warehousecode='" + key + "'");
                        if (drs_D != null && drs_D.Length > 0)
                        {
                            sbskusubtotal.Append(drs_D[0]["TotalQty"].ToString() + ",," + string.Format("{0:N2}", drs_D[0]["Totalextended"]).Replace(",", "") + ",");
                            try
                            {
                                tQty = int.Parse(drs_D[0]["TotalQty"].ToString());
                                tExt = double.Parse(drs_D[0]["Totalextended"].ToString());
                                totalqty = totalqty + tQty;
                                totalExt = totalExt + tExt;
                            }
                            catch { }
                        }
                        else
                        {
                            sbskusubtotal.Append(",,,");
                        }

                        sbskusubtotal.Append(",");

                    }
                    sbskusubtotal.Append(totalqty.ToString() + ",," + string.Format("{0:N2}", totalExt).Replace(",", "") + ",");
                    sbskusubtotal.Append("\r\n");
                    tw.Write(sbskusubtotal.ToString());
                }
                StringBuilder sbskunothearder = new StringBuilder();
                int whNum = dt_wh.Rows.Count;
                int ColSpanNum = 14 + whNum * 3;
                for (int i = 0; i < ColSpanNum; i++) { sbskunothearder.Append(","); }
                sbskunothearder.Append("\r\n");
                sbskunothearder.Append("Trucks Arrived but not Received In:,,");
                sbskunothearder.Append("Truck ARR Date,");
                sbskunothearder.Append("Receive Date,");
                sbskunothearder.Append(",");
                int tempNum = ColSpanNum - 5;
                for (int i = 0; i < tempNum; i++) { sbskunothearder.Append(","); }
                sbskunothearder.Append("\r\n");
                tw.Write(sbskunothearder.ToString());
                // not in
                if (ds_notIn != null && ds_notIn.Tables.Count == 2)
                {
                    dtnot_d = ds_notIn.Tables[0]; // data
                    dtnot_s = ds_notIn.Tables[1]; // subtotal
                    // data
                    if (dtnot_d != null && dtnot_d.Rows.Count > 0)
                    {
                        StringBuilder sbskunotdata = new StringBuilder();
                        int skunotindex = 0;
                        int notindex = 0;
                        int notskucount = dtnot_d.Rows.Count;
                        foreach (DataRow dr in dtnot_d.Rows)
                        {
                            double ext = 0;
                            sbskunotdata.Append(dr["ReceiveNumber"].ToString() + ",");
                            sbskunotdata.Append(dr["vendorname"].ToString().Replace(",", " ") + ",");
                            sbskunotdata.Append(string.Format("{0:d}", dr["TruckArriveDate"]) + ",");
                            sbskunotdata.Append(string.Format("{0:d}", dr["ReceiveDate"]) + ",");
                            sbskunotdata.Append(",");
                            sbskunotdata.Append(",");
                            foreach (DataRow drw in dt_wh.Rows)
                            {
                                string key = drw["warehousecode"].ToString().Trim();
                                if (dr["warehousecode"].ToString().Trim().ToUpper() == key.ToUpper().Trim())
                                {

                                    sbskunotdata.Append(",," + dr["InvoiceAmount"].ToString() + ",");
                                    try { ext = double.Parse(dr["InvoiceAmount"].ToString()); }
                                    catch { }
                                }
                                else
                                {
                                    sbskunotdata.Append(",,,");
                                }
                                sbskunotdata.Append(",");
                            }

                            sbskunotdata.Append(",," + string.Format("{0:N2}", ext).Replace(",", "") + ",");
                            sbskunotdata.Append("\r\n");
                            skunotindex = skunotindex + 1;
                            notindex = notindex + 1;
                            if (skunotindex == 1000 || notindex == notskucount)
                            {
                                tw.Write(sbskunotdata.ToString());
                                skunotindex = 0;
                                sbskunotdata = new StringBuilder();

                            }
                        }
                    }

                    // summary row in Not in
                    StringBuilder sbskunotsummary = new StringBuilder();
                    if (dtnot_s != null && dtnot_s.Rows.Count > 0)
                    {
                        tExt = 0;
                        totalExt = 0;
                        sbskunotsummary.Append("Subtotal:,,,,,");
                        sbskunotsummary.Append(",");
                        foreach (DataRow drw in dt_wh.Rows)
                        {
                            string key = drw["warehousecode"].ToString().Trim();
                            drs_D = dtnot_s.Select("warehousecode='" + key + "'");
                            if (drs_D != null && drs_D.Length > 0)
                            {
                                sbskunotsummary.Append(",," + string.Format("{0:N2}", drs_D[0]["Totalext"]).Replace(",", "") + ",");
                                try
                                {
                                    tExt = double.Parse(drs_D[0]["Totalext"].ToString());
                                    totalExt = totalExt + tExt;
                                }
                                catch { }
                            }
                            else
                            {
                                sbskunotsummary.Append(",,,");
                            }

                            sbskunotsummary.Append(",");
                        }
                        sbskunotsummary.Append(",," + string.Format("{0:N2}", totalExt).Replace(",", "") + ",");
                        sbskunotsummary.Append("\r\n");
                    }
                    for (int i = 0; i < ColSpanNum; i++) { sbskunotsummary.Append(","); }
                    sbskunotsummary.Append("\r\n");
                    tw.Write(sbskunotsummary.ToString());
                }

                // Grand TotalInvetory
                StringBuilder sbtotal = new StringBuilder();
                double totalLocExt = 0, totalSum = 0;
                totalExt = 0;
                sbtotal.Append("Total Inventory:,,,,,");
                sbtotal.Append(",");
                foreach (DataRow drw in dt_wh.Rows)
                {
                    string key = drw["warehousecode"].ToString().Trim();
                    totalLocExt = 0; totalSum = 0;

                    if (dtnot_s != null && dtnot_s.Rows.Count > 0)
                    {
                        drs_D = dtnot_s.Select("warehousecode='" + key + "'");
                        if (drs_D != null && drs_D.Length > 0)
                        {
                            try
                            {
                                totalLocExt = double.Parse(drs_D[0]["Totalext"].ToString());
                            }
                            catch
                            {
                                totalLocExt = 0;
                            }
                            totalExt = totalExt + totalLocExt;
                        }
                    }
                    if (dt_sum != null && dt_sum.Rows.Count > 0)
                    {
                        drs_D = dt_sum.Select("warehousecode='" + key + "'");
                        if (drs_D != null && drs_D.Length > 0)
                        {
                            try
                            {
                                totalSum = double.Parse(drs_D[0]["Totalextended"].ToString());
                            }
                            catch
                            {
                                totalSum = 0;
                            }
                            totalExt = totalExt + totalSum;
                        }
                    }
                    sbtotal.Append(",," + string.Format("{0:N2}", totalLocExt + totalSum).Replace(",", "") + ",");
                    sbtotal.Append(",");
                }

                sbtotal.Append(",," + string.Format("{0:N2}", totalExt).Replace(",", "") + ",");
                sbtotal.Append("\r\n");
                tw.Write(sbtotal.ToString());
                tw.Flush();
                tw.Close();
                tw = null;
                this.Msg.Text = "The file is saved at " + _path + filename + ", please get form there.";
            }
        }

    }

 

这个SIZE比上面的小点,而且EXCEL标题控制比较灵活,但一次写入文件中数据多也抗不住,可以分多次写缓解下。

3.把EXCEL当数据库操作

增删改查参考举例:

http://davidhayden.com/blog/dave/archive/2006/05/26/2973.aspx

批量处理链接

http://www.codeproject.com/Articles/17028/Bulk-Record-Insert-for-Access

 

这里有个问题就是这里把EXCEL当着表来处理,所以如果是多重标题就无法满足。

 

小知识点:

当产品文件无法让你生成EXCEL时,你可以tmp目录下生成excel, 然后载入这个excel的内容输入到response让用户下载

posted @ 2012-04-11 18:31  scott.pei  阅读(329)  评论(0编辑  收藏  举报