顯示具有 列印 標籤的文章。 顯示所有文章
顯示具有 列印 標籤的文章。 顯示所有文章

2022年11月2日 星期三

jQuery - printThis - jQuery元件 vs Ext.getCmp 元件的比較 - $("#sub_panel" )取得的元件,才可append

目的: 預覽資料後列印 - jQuery printThis

處理說明: 1>由 jQuery 取得 printArea 元件

                  2>利用 jQuery元件的 printThis() 函式, 預覽列印結果

                  3>jQuery元件和 Ext.getCmp() 元件的不同(兩種函式庫的Object  property 定義不同)



1>*.js
 var sub_PrnTableFlds = [
        {
            type: 'panel', bodyStyle: "background-color:transparent;", border: 0, padding: "5",
            flex: 100,
            //layout: { type: 'vbox', align: 'stretch' }, 
            layout: 'border',
            items: [
                {  //sub_panel1 : table panel
                    xtype: 'panel',
                    id: 'sub_panel1',
                    //region: 'north',
                    border: true,
                    layout: { type: 'hbox', align: 'stretch' },                    
                    items: [],
                },  //end of panel1                                                    
            ] // end of   layout: "vbox", padding: "5", items: [
        }  //end of  sub_PrnTableFlds , items[{
    ]      //end of  sub_PrnTableFlds , items[    

 var win = getMyWindow("PrnTable測試", sub_PrnTableFlds, sub_PrnTableFlds_Btns);
    win.setWidth(600);
    win.setHeight(400);
    win.show();
    console.log("step6");
} // end of   function LendBtn_click() {

// 標籤列印 - 於 sub_panrl1 建立 Table
function mySub1_OkBtn2_click() {
    var Tmp_Str = "<div  id='print_area'   style = 'border: 1px solid black; width=100%;' >"
        + "<table style = 'border: 1px solid black; width=100%;'  id = 'my_tbl' > "        
        + "<tr style='border: 1px solid black;'><th>件號<br>(PART NO)</th><td>R=8-84-766-21-1</td></tr>"
        + "<tr><th>EO號碼<br>(EONO)</th><td>GM1381+A15</td></tr>"
        + "</table>"
        +"</div>";
    //var Tmp_tbl = Ext.getCmp("my_tbl");
    var Tmp_tbl = $("#my_tbl");
    if (!checkisnull(Tmp_tbl)) {
        console.log("Tmp_tbl is captched");
        console.log("Tmp_tbl.style:",Tmp_tbl.style);
        Tmp_tbl.style = "{border: 2px solid red;}";
    }
    else 
        console.log("Tmp_tbl is null");
    //var Tmp_area = Ext.getCmp("print_area");
    var Tmp_area = $("#print_area");
    if (!checkisnull(Tmp_area)) {
        console.log("Tmp_area is captched");
        console.log("Tmp_area.style:", Tmp_area.style);
        Tmp_area.style = "{border: 2px solid red;}";
    }
    else
        console.log("Tmp_area is null");
    var Tmp_sub_panel1 = $('#sub_panel1');
    //var Tmp_sub_panel1 = Ext.getCmp('sub_panel1');
    if (!checkisnull(Tmp_sub_panel1)) {
        console.log("Tmp_sub_panel1 is not null");
        console.log("Tmp_sub_panel1", Tmp_sub_panel1);
    }
    Tmp_sub_panel1.append(Tmp_Str);
    //Tmp_area.printThis({});
    $('#print_area').printThis({ });    
    //$('#sub_panel1').remove();
}


2>Ext.getCmp("sub_panel1") 取得的元件,並沒有 append(), appendTo(), printThis() 函式
    只有 $("#sub_panel1");  jQuery取得的元件才有 append(), appendTo(), printThis()函式
    --> 即  jQuery $("#sub_panel1") 和 Ext.getCmp("sub_panel1") 取得的 Object 定義不同



2022年8月17日 星期三

V120502 - 套表列印, 並自動分頁 , 顯示 Cookie的訊息 , 顯示請稍候訊息

 目的: V120502 - 套表列印, 並自動分頁

處理說明 : 
      1>前端:
                  1>>將子畫面的Grid資料存成 JSON字串, 當np參數傳給 Ext.Ajax 
      2>後端
                  1>>讀取 SS_FILES 套表(using Spire.Xls;)
                  2>>讀取 np 參數中的子畫面資料
                  3>>開啟 *.xlsx 檔案,填入欄位值, 若筆數>每頁筆數,則跳頁  




1>*.js  將子畫面的資料轉成 JSON 字串, 並存成 np參數 , 傳給 Ext.Ajax

//[確定]鈕 - 套表列印
function mySub1_OkBtn_click() {                
        //0 > 將目前畫面的store資料, 存成 np(多筆資料) 當參數, 傳給 處理 url
        console.log("0 mySub1_OkBtn_click - 列印包裝標籤.確定");
        var Tmp_sub_store = Ext.getCmp('sub_Grid').store;
        var cur_rec;
        var Tmp_SAPNO, Tmp_PN, Tmp_QTY, Tmp_EOCND, Tmp_RMK;
        var Tmp_data_JSON = "";  //將store資料轉成 JSON 的字串
        var Tmp_sub_np = {};        
        let Tmp_DataAry1 = [];
        var Tmp_Str="";
        for (i = 0; i < Tmp_sub_store.getCount(); i++) {
            cur_rec = Tmp_sub_store.getAt(i);
            console.log(Tmp_Str + "cur_rec:", cur_rec);
            Tmp_DataAry1.push(cur_rec.data);            
        }
        console.log("Tmp_DataAry1:", Tmp_DataAry1);        
        // 將目前子畫面的資料(Table)轉成 JSON字串
        let Tmp_DataString1 = JSON.stringify(Tmp_DataAry1);
        console.log("Tmp_DataString1:", Tmp_DataString1);

        var Tmp_CHKDT = Ext.getCmp("sub_DT").getValue();
        console.log("Tmp_CHKDT:", Tmp_CHKDT);
        var np = {};
        np = {
            DataString1: Tmp_DataString1,  
            CHKDT: Tmp_CHKDT,
        };        

        Ext.getCmp('mySubForm').submit({
            //Ext.Ajax.request({
            url: '../../api/V120502API/DoPRNLIST',
            method: 'POST',
            async: false,
            standardSubmit: true, //若要傳送檔案至前端, standardSubmit必需設為 true  
            params: np,
        });
        //顯示結果訊息..

        var mask = new Ext.LoadMask(Ext.getBody(), {
            msg: '處理中,請稍待...'
        });

        mask.show();//使用 mask 需手動呼叫show() 方法下
        var timer = setInterval(function () {
            clearInterval(timer);
            mask.hide();
            timer = null;
            var r = r_cookies('EX_DFile');
            console.log("r_cookies=", r);
            if (!checkisnull(r)) {
                mysuccessalert(r);
            }
        }, 5000);  //3000ms = 3sec
    };  // end of function mySub1_OkBtn_click()


2>*.cs 處理
[HttpPost]
        public void DoPRNLIST()
        {
            var c = HttpContext.Current;
            NameValueCollection nvc = c.Request.Form;
            // 1>將 SS_FILES.FBOLD 存成 Local檔案
            //取得目前子畫面的欄位值
            string Tmp_DataString1 = nvc["DataString1"];    //Tmp_DataString:  將目前子畫面的資料. JSON字串   
            string Tmp_CHKDT = nvc["CHKDT"];    //[ ]不印日期
//將 table 字串轉成 DataTable
DataTable DataTable1 = (DataTable)JsonConvert.DeserializeObject(Tmp_DataString1, (typeof(DataTable)));

            string Tmp_SAPNO, Tmp_PN, Tmp_EOCND, Tmp_RMK;
            string Tmp_QTY;            
            
                        OracleConnection conn = new OracleConnection(DBService.ConnectionString(DBLINK));//
                        OracleCommand cmd = new OracleCommand();
                        //OracleDataAdapter adp = new OracleDataAdapter();
                        //DataSet ds = new DataSet();
                        //var response = Request.CreateResponse();

                        conn.Open();
                        conn.ClientInfo = User.Identity.Name;
                        conn.ModuleName = BaseSYS + "_" + BaseMODID;
                        conn.ActionName = ActionName;
                        cmd.Connection = conn;
                        string Tmp_Sql = "";
                        string Tmp_Str = "";
                        string Tmp_Str1 = "";
                        string Tmp_RtnMsg;
                        string Tmp_FName = "V120502_包裝標籤_格式檔.xlsx";      //套表的檔案名稱 , 
                        string documentPath = HttpContext.Current.Server.MapPath("~") + "document\\";  // 取得實實的路徑            
                        string pathFName = documentPath + Tmp_FName;
                        //需要擷取大量資料時,DataReader 是很好的選擇,因為資料不會快取至記憶體。
                        OracleDataReader reader;
                        Workbook wk = null;
                        MemoryStream mstream = new MemoryStream();
                        string FileName = "V120502_包裝標籤_格式檔_" + DateTime.Now.ToString("yyyyMMdd") + ".xlsx";
                        string FileName1 = documentPath + FileName;
                        try
                        {
                            wk = new Workbook();
                            //1>將  SS_FILES. FBODY , 存至Local目錄.檔案                 
                            if (File.Exists(pathFName))
                            {
                                File.Delete(pathFName);
                            }
                            Tmp_Sql = " SELECT  FNAME,FBODY "
                                            + " FROM     SS_FILES   "
                                            + "   WHERE   1 = 1  "
                                            + "  AND         FNAME = " + myfunc.AA(Tmp_FName);
                            cmd.CommandText = Tmp_Sql;
                            reader = cmd.ExecuteReader();
                            byte[] file = null;
                            //FileMode.OpenOrCreate: 開啟檔案若無檔案則新增
                            //FileAccess.ReadWrite: 允許檔案讀取/寫入
                            FileStream fs = File.Open(pathFName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                            BinaryWriter bw = new BinaryWriter(fs);
                            if (reader.HasRows)
                            {
                                if (reader.Read())
                                {
                                    file = (byte[])reader["FBODY"];
                                    //fs.Write(file,0,50000);
                                    bw.Write(file);
                                    wk.LoadFromStream(fs);
                                    bw.Close();
                                    fs.Close();
                                }
                            }

                            //2 > 主機.Local檔案 XLS 讀入  FStream
                            Worksheet ws = wk.Worksheets[0];//獲取第一個工作表
                                                            //寫入[SAPNO][PN][QTY][EOCND][RMK]

                int RowCnt = 0;  //每頁 行                    
                int PageRowIndex = 0;  //本頁啟始 rowindex
                int PageSize = 8;            //每頁8行                       
                //每筆資料列印後, 分頁印下一筆
                for (var i = 0; i < DataTable1.Rows.Count; i++)
                            {
                    //每筆資料均分頁 & 複製樣板 rows                    
                        PageRowIndex = PageRowIndex + PageSize;
                        ws.HPageBreaks.Add(ws.Range[myfunc.GetExcelPos(0, PageRowIndex)]);
                        string Tmp_Range = "A" + string.Format("{0:D}", PageRowIndex + 1) + ":B" + string.Format("{0:D}", PageRowIndex + 1 + (PageSize - 1));
                        ws.Copy(ws.Range["A1:B8"], ws.Range[Tmp_Range], true);
                    
                                Tmp_SAPNO = DataTable1.Rows[i]["SAPNO"].ToString();
                                Tmp_PN = DataTable1.Rows[i]["PN"].ToString();
                                Tmp_QTY = DataTable1.Rows[i]["QTY"].ToString();
                                Tmp_EOCND = DataTable1.Rows[i]["EOCND"].ToString();
                                Tmp_RMK = DataTable1.Rows[i]["RMK"].ToString();
                                ws.Range[myfunc.GetExcelPos(1, PageRowIndex)].Text = Tmp_PN;
                                ws.Range[myfunc.GetExcelPos(1, PageRowIndex + 1)].Text = Tmp_EOCND;
                                if (Tmp_CHKDT == "true")  //不印日期
                    {
                        ws.Range[myfunc.GetExcelPos(1, PageRowIndex + 2)].Text = "";
                    }
                                else
                    {
                        ws.Range[myfunc.GetExcelPos(1, PageRowIndex + 2)].Text = myfunc.cnow();                        
                    }
                        
                                 ws.Range[myfunc.GetExcelPos(1, PageRowIndex + 3)].Text = Tmp_QTY;
                                 ws.Range[myfunc.GetExcelPos(1, PageRowIndex + 4)].Text = Tmp_SAPNO;
                                 ws.Range[myfunc.GetExcelPos(1, PageRowIndex + 5)].Text = "";  //品保欄位空白
                                 ws.Range[myfunc.GetExcelPos(1, PageRowIndex + 6)].Text = Tmp_RMK;                                                                  
                }                     
                            //刪除樣板  rows                            
                            ws.DeleteRow(1, PageSize);
                            //ws.AllocatedRange.AutoFitColumns();
                            //wk.SaveToStream(mstream, FileFormat.Version2007);
                            //wk.SaveToFile(FileName1, FileFormat.Version2007);
                wk.SaveToStream(mstream, FileFormat.Version2013);
                wk.SaveToFile(FileName1, FileFormat.Version2013);
                //wk.SaveToFile(FileName, ExcelVersion.Version2007);                
            }
                        catch (Exception e)
                        {
                            var Tmp_ErrMsg = e.Message;
                        }
                        finally
                        {
                            //reader.Close();
                            //bw.Close();
                            //fs.Close();
                            wk.Dispose();
                            conn.Close();
                            //wk.close();

                        }

                        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + HttpUtility.UrlEncode((string)FileName, System.Text.Encoding.UTF8) + "\"");
                        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
                        HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                        HttpContext.Current.Response.BinaryWrite(mstream.ToArray());
            //包裝標籤檔產生完成
                       Tmp_Str = "請開啟如下XLS檔案<br>" 
                                       +"(" + FileName + ")<br>"
                                        +"並挑選[標籤印表機(TSC_ME240)]印表機<br>" 
                                        +"標籤樣式100mm*60mm(寬*高)列印<br>";
                        HttpCookie MyCookie = new HttpCookie("EX_DFile", HttpUtility.UrlEncode(Tmp_Str));
                        HttpContext.Current.Response.Cookies.Add(MyCookie);
                        HttpContext.Current.Response.End();                        
        }  // end of public void DoPRNLIST()




2022年7月21日 星期四

C# 利用 iTextSharp 產生 PDF檔案

使用ASP .NET (C#) 產生PDF檔的好幫手—iTextSharp library (上)

 https://www.cc.ntu.edu.tw/chinese/epaper/0015/20101220_1509.htm

Question: Web程式 – 如何執行[標籤]的印表機列印 - 產生PDF檔的好幫手—iTextSharp

 

目的: 如何在 Web程式,如何執行[標籤]的印表機列印

說明:  1> 目前 Web程式(Browser端) 無法取得 本機印表機清單
           2>   是否可先產生 XLS檔案下載至前端, 再由使用者自行列印

目前問題: 1>網頁安全的條件下已不允許伺服器後端的程式去控制前端印表機,
                    連下載檔案以關聯方式啟動程式的方式都不被允許,
                    除非你能找到夠舊的IE透過ACTIVEX來做這些事,但....應該會被罵吧


處理說明:
    1>標籤大小: 100mm*60mm
        邊界: 上右下左  : 1.5mm*1.5mm*1.5mm*1.5mm 
    2>試著產生 xls 檔,並由使用者開啟 xls 後, 自行列印