• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 195,  comments - 30,  trackbacks - 0

            源地址:http://jackyi.javaeye.com/blog/787442
            c#調用excel模板頁,然后套用模板頁導出多個sheet

            文章分類:.net編程
            //明細導出
                    protected void btn_DetailExport_Click(object sender, EventArgs e)
                    {
                        int count1 = this.ASPxGridView1.VisibleRowCount;
                        //Response.Redirect("../Quote/QuoteOutput.aspx");
                        //先判斷網格是否為空,或者當數據超過255條是提示數據過多。
                        if (count1 == 0)
                        {
                            Response.Write("<script>'alert('網格沒有數據,不能導出')</script>");
                        }
                        if (count1 > 255)
                        {
                            Response.Write("<script>'alert('數據量超過255條,不能導出')</script>");
                        }
                        string filepath = null;
                        GC.Collect();
                        //獲得模板頁的路徑
                        string strFileName = Server.MapPath(Request.ApplicationPath) + @"ShoesPic\template.xls";
                        Application excel = new Application();//用于導出
                        Workbook xBk;
                        excel.Visible = true;
                        excel.UserControl = true;
                        object missing = System.Reflection.Missing.Value;
                        //加載Excel模板文件
                        xBk = excel.Workbooks.Open(strFileName, missing, true, missing, missing, missing, missing, missing, missing, true, missing, missing, missing, missing, missing);
                        Worksheet xSt;
                        // 復制count1-1個WorkSheet對象
                        for (int i = 1; i < count1; i++)
                        {
                            ((Worksheet)xBk.Worksheets.get_Item(i)).Copy(missing, xBk.Worksheets[i]);
                        }
                        if (excel == null)
                        {
                            Response.Write("<script>alert('Can't access excel')</script>");
                        }
                        else
                        {
                            try
                            {
                                //xSt.Name = "報價單導出"; //這里修改sheet名稱
                                //獲得ASPxGridView的所有數據
                                for (int j = 0; j < count1; j++)
                                {
                                    xSt = (Worksheet)xBk.Worksheets.get_Item(j+1);
                                    //xSt = (Worksheet)xBk.Sheets[j + 1];//第一個sheet頁
                                    object data = this.ASPxGridView1.GetRowValues(j, "報價單編號");//獲得主鍵數據
                                    xSt.Name = data.ToString();
                                    Basic.QuoteManager manager = new Basic.QuoteManager();
                                    Model.Quote quote = manager.getQuotevalueByNo(data.ToString());
                                    string providername = quote.ProviderName;
                                    string brand = quote.Brand;
                                    string colthnob = quote.Colthnob;
                                    string q_user = quote.Q_User;
                                    string phone = quote.Phone;
                                    string mobile = quote.Mobile;
                                    string c_user = quote.C_User;
                                    string principal = quote.Principal;
                                    string leader = quote.Leader;
                                    string picurl = quote.PicUrl;
                                    string user = quote.User;
                                    string remark = quote.Remark;
                                    string q_date = quote.Q_Date.ToString();
                                    string contract_price = quote.Contract_Price.ToString();
                                    //報價單位
                                    xSt.get_Range("B2", missing).Value2 = providername;
                                    //品牌
                                    xSt.get_Range("B3", missing).Value2 = brand;
                                    //款號/工廠貨號
                                    xSt.get_Range("B4", missing).Value2 = colthnob;
                                    //報價人
                                    xSt.get_Range("B5", missing).Value2 = q_user;
                                    //聯系電話
                                    xSt.get_Range("B6", missing).Value2 = phone;
                                    //移動電話
                                    xSt.get_Range("B7", missing).Value2 = mobile;
                                    //日期/報價日期
                                    xSt.get_Range("D2", missing).Value2 = q_date;
                                    //核價人員
                                    xSt.get_Range("D3", missing).Value2 = c_user;
                                    //品牌負責人
                                    xSt.get_Range("D4", missing).Value2 = principal;
                                    //分管領導
                                    xSt.get_Range("D5", missing).Value2 = leader;
                                    //圖片/圖片地址
                                    xSt.get_Range("D6", missing).Value2 = picurl;
                                    //制單人
                                    xSt.get_Range("D7", missing).Value2 = user;
                                    //合同價
                                    xSt.get_Range("F2", missing).Value2 = contract_price;
                                    //備注
                                    xSt.get_Range("F3", missing).Value2 = remark;

                                    ////明細表數據
                                    Basic.QuoteManager manager1 = new Basic.QuoteManager();
                                    Model.QuoteDetail[] quotedetail = manager1.getQuoteDetailvalueByNo(data.ToString());
                                    int count = quotedetail.Length;
                                    int rowsint = 10;
                                    foreach (Model.QuoteDetail i in quotedetail)
                                    {
                                        //item 項目
                                        xSt.Cells.get_Range("A" + rowsint, missing).Value2 = i.Item;
                                        //itemname 名稱
                                        xSt.Cells.get_Range("B" + rowsint, missing).Value2 = i.Itemname;
                                        //unit 單位
                                        xSt.Cells.get_Range("C" + rowsint, missing).Value2 = i.Unit;
                                        //q_dosage 單用量
                                        xSt.Cells.get_Range("D" + rowsint, missing).Value2 = i.Q_Dosage;
                                        // q_unitprice 單價
                                        xSt.Cells.get_Range("E" + rowsint, missing).Value2 = i.Q_UnitPrice;
                                        //q_price 金額
                                        xSt.Cells.get_Range("F" + rowsint, missing).Value2 = i.Q_Price;
                                        rowsint++;
                                    }
                                    //先清除模板頁的數據,然后新增一個sheet
                                   
                                    //對一個sheet賦值后,需要新增一個sheet,然后再賦值操作
                                    //xSt = (Worksheet)excel.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                                }
                                //保存的時候會先保存在服務器上,然后再從服務器下載下來。
                                filepath = Server.MapPath(Request.ApplicationPath) + @"ShoesPic\報價單導出.xls";
                                xBk.Saved = true;
                                xBk.SaveCopyAs(filepath);
                                //退出excel
                                excel.Quit();
                                excel = null;
                                xBk = null;
                                xSt = null;
                                Process[] procs = Process.GetProcessesByName("excel");

                                foreach (Process pro in procs)
                                {
                                    pro.Kill();//沒有更好的方法,只有殺掉進程
                                }
                                GC.Collect();

                                System.IO.FileInfo file = new System.IO.FileInfo(filepath);
                                Response.Clear();
                                Response.Charset = "GB2312";
                                Response.ContentEncoding = System.Text.Encoding.UTF8;
                                // 添加頭信息,為"文件下載/另存為"對話框指定默認文件名
                                Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
                                // 添加頭信息,指定文件大小,讓瀏覽器能夠顯示下載進度
                                Response.AddHeader("Content-Length", file.Length.ToString());

                                // 指定返回的是一個不能被客戶端讀取的流,必須被下載
                                Response.ContentType = "application/ms-excel";

                                // 把文件流發送到客戶端
                                Response.WriteFile(file.FullName);
                                Response.Flush();//這個語句必須有,否則就不回彈出保存的對話框,搞了N久
                                // 停止頁面的執行
                                Response.End();
                               
                            }
                            catch (Exception ex)
                            {
                            }
                            finally
                            {
                                if (File.Exists(filepath))
                                {
                                    File.Delete(filepath);
                                }
                            }
                        }
                    }

            posted on 2011-03-21 13:57 luis 閱讀(1919) 評論(1)  編輯 收藏 引用

            FeedBack:
            # re: c#調用excel模板頁,然后套用模板頁導出多個sheet
            2012-05-25 11:40 | 未名
            xBk = excel.Workbooks.Open(strFileName, missing, true, missing, missing, missing, missing, missing, missing, true, missing, missing, missing, missing, missing);

            執行這一步就報錯  回復  更多評論
              
            <2009年7月>
            2829301234
            567891011
            12131415161718
            19202122232425
            2627282930311
            2345678

            常用鏈接

            留言簿(3)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            友情鏈接

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            久久美女网站免费| 亚洲欧美成人综合久久久| 91精品国产高清久久久久久国产嫩草 | 亚洲国产精品18久久久久久| 亚洲va中文字幕无码久久不卡| 日本久久久久亚洲中字幕 | 久久只这里是精品66| 久久这里只有精品首页| 2021精品国产综合久久| 亚洲欧洲久久av| 青青青国产成人久久111网站| 人妻无码久久精品| 久久国产精品久久| 欧洲成人午夜精品无码区久久| 99久久精品国产一区二区蜜芽| 国产成人精品三上悠亚久久| 国产成人综合久久久久久| 人妻精品久久久久中文字幕69 | 久久精品国产2020| 97精品国产97久久久久久免费| 狠狠色丁香婷婷久久综合五月 | 日日噜噜夜夜狠狠久久丁香五月| 久久www免费人成精品香蕉| 一本色综合网久久| 亚洲国产小视频精品久久久三级| 国产人久久人人人人爽| 欧美激情一区二区久久久| 色综合久久精品中文字幕首页| 日日躁夜夜躁狠狠久久AV| 久久久亚洲欧洲日产国码是AV| 精品久久久久久久中文字幕| 99久久人妻无码精品系列蜜桃| 影音先锋女人AV鲁色资源网久久| 久久精品无码av| 国产高潮久久免费观看| 久久综合狠狠综合久久激情 | 国内精品久久久人妻中文字幕| 国产精品中文久久久久久久| 一级做a爰片久久毛片看看| 开心久久婷婷综合中文字幕| 久久99精品久久久久久噜噜|