国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

C#標(biāo)簽打印示例1

2019-11-14 14:12:06
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
初次寫博客,有哪些不足的地方,還請(qǐng)多多指點(diǎn),給予建議,謝謝!
如若想要源碼,請(qǐng)留言。
    
圖片
 
本實(shí)例是在Webservice 中通過(guò)Excel模板來(lái)打印標(biāo)簽。
具體需求是:一個(gè)訂單一頁(yè)紙打印4行分錄,如果超過(guò)4行,則再次按照原格式換紙打印,如果行數(shù)未滿4行,則補(bǔ)空行。
一、實(shí)現(xiàn)步驟:
1、首先在EXCEL 畫(huà)好模版 (后綴名是 .xlt )
2、在程序中調(diào)用EXCEL 填充數(shù)據(jù)
3、調(diào)用EXCEL打印方法打印  

二、源碼重點(diǎn)講解:
1、List<> 泛型集合保存打印的數(shù)據(jù),通過(guò)它可以刪除已經(jīng)打印過(guò)的數(shù)據(jù)
 示例:
for (int i = 0; i <= list.Count; i++)
 {
        if (i > 0) i--; //每當(dāng)執(zhí)行完下一次的時(shí)候,將這個(gè)值初始化為0,即i-- 
        if (nindex == Convert.ToInt32(pagesize)) break;  //如果超過(guò)指定的行數(shù),則跳出循環(huán)
        FitemData model = list[i]; 
        ..........
        list.Remove(model); //移除當(dāng)前行
}

2、While 循環(huán):當(dāng)條件為真的時(shí)候執(zhí)行循環(huán)體,先判斷后執(zhí)行,循環(huán)次數(shù)不固定
示例:
 while (list.Count > 0)
 {
        PRintExccel(ref list, damountcount.ToString(), FirstPageText.ToString(), PageCount.ToString(), PageSize.ToString(), ref strmes);
        FirstPageText++;
 } 
=========================源碼講解==============================
 /// <summary>
 /// 打印方法
 /// </summary>
 /// <param name="dt">打印的數(shù)據(jù)</param>
 /// <param name="strmes">bug返回的異常信息</param>
 private void Print(DataTable dt, ref string strmes)
 {
        List<FitemData> list = new List<FitemData>();
        decimal damountcount = 0.00M; //合計(jì)小寫金額
        try
        {
            foreach (DataRow dr in dt.Rows)
            {
                FitemData model = new FitemData();
                model.F_106 = dr["F_106"].ToString();
                model.FAmount = Convert.ToDecimal(dr["FAmount"].ToString()).ToString("f2");
                model.FPrice = Convert.ToDecimal(dr["FPrice"].ToString()).ToString("f2");
                model.FUnitName = dr["FUnitName"].ToString();
                model.Fdate = dr["fdate"].ToString();
                model.Fbillno = dr["fbillno"].ToString();
                model.FQty = dr["FQty"].ToString();
                damountcount += Convert.ToDecimal((model.FAmount));
                list.Add(model);
            } 
 
            int PageSize = Convert.ToInt32(ConfigurationManager.AppSettings["PageSize"]);    //每頁(yè)行數(shù)
            int PageCount = 0;  //總頁(yè)數(shù)
            int FirstPageText = 1; //首頁(yè)
            PageCount = list.Count % PageSize > 0 ? list.Count / PageSize + 1 : list.Count / PageSize;
 
            while (list.Count > 0)
            {
                PrintExccel(ref list, damountcount.ToString(), FirstPageText.ToString(), PageCount.ToString(), PageSize.ToString(), ref strmes);
                FirstPageText++;
            }
        }
        catch (Exception ex)
        {
            strmes = ex.Message;
        }
   }
 
/// <summary>
/// excel 打印
/// </summary>
/// <param name="list">總記錄數(shù)</param>
/// <param name="damountcount">小寫金額總計(jì)</param>
/// <param name="firsrpagetext">頁(yè)碼數(shù)</param>
/// <param name="pagesize">每頁(yè)行數(shù)</param>
/// <param name="strmes">錯(cuò)誤參數(shù)回寫</param>
private void PrintExccel(ref List<FitemData> list, string damountcount, string firsrpagetext, string pagecount, string pagesize, ref string strmes)
{
        try
        {  
            int rowBase = 4,colBase = 1,rowIndex = rowBase,colIndex = colBase, nindex = 0;
            decimal dfamount = 0.0M;  
  
            Excel.application excelapp;
            excelapp = new Microsoft.Office.Interop.Excel.Application();
            Excel.Workbook book = excelapp.Workbooks.Open(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"/ReportFile/Report.xlt",
                Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
 
            Excel.Worksheet st1 = (Excel.Worksheet)book.Worksheets[1]; // 選擇的工作表,序號(hào)默認(rèn)是從1開(kāi)始即(Sheet0)
            st1.Cells[2, 2] = list[0].Fdate;
            st1.Cells[2, 7] = list[0].Fbillno;
            st1.Cells[3, 1] = "商品名稱";   
            for (int i = 0; i <= list.Count; i++)
            {
                if (i > 0) i--;
                if (nindex == Convert.ToInt32(pagesize)) break;  //如果超過(guò)指定的行數(shù),則跳出循環(huán)
                FitemData model = list[i];
 
                //復(fù)制格式 插入一個(gè)新行
                Excel.Range range = (Excel.Range)st1.Rows[rowBase, Missing.Value];
                range.Insert(Excel.XlInsertShiftDirection.xlShiftDown, Missing.Value);
 
                st1.Cells[rowBase, colIndex] = model.F_106;
                colIndex = colIndex + 2;
                st1.Cells[rowBase, colIndex] = model.FUnitName;
                colIndex++;
                st1.Cells[rowBase, colIndex] = "'" + model.FQty;
                colIndex++;
                st1.Cells[rowBase, colIndex] = "'" + model.FPrice;
                colIndex++;
                st1.Cells[rowBase, colIndex] = "'" + model.FAmount;
            
                rowBase++;
                colIndex++;
                rowIndex++;
                colIndex = colBase;
                dfamount += Convert.ToDecimal(model.FAmount);  
                nindex++;
                list.Remove(model);  
            }
 
            if (nindex < Convert.ToInt32(pagesize))
            {
                for (int i = 0; i < Convert.ToInt32(pagesize) - nindex; i++)
                {
                    //復(fù)制格式 插入一個(gè)新行
                    Excel.Range range = (Excel.Range)st1.Rows[rowBase, Missing.Value];
                    range.Insert(Excel.XlInsertShiftDirection.xlShiftDown, Missing.Value);
                    rowBase++;
                    colIndex++;
                    rowIndex++;
                    colIndex = colBase;
                }
            } 
 
            Excel.Range rangedel = (Excel.Range)st1.Rows[rowIndex, Missing.Value];
            rangedel.EntireRow.Delete(Excel.XlDeleteShiftDirection.xlShiftUp);   //刪除多余行 (即固定訂單分錄行數(shù)下多出的一行)
 
            st1.Cells[rowIndex, 2] = "'" + damountcount; //dfamount.ToString("f2");
            st1.Cells[rowIndex, 6] = "'" + dfamount.ToString("f2");
            st1.Cells[rowIndex + 1, 2] = MoneyToUpper(Convert.ToDecimal(damountcount).ToString("f2"));
            st1.Cells[rowIndex + 1, 7] = "第  " + firsrpagetext + "  聯(lián)";
            st1.Cells[rowIndex + 2, 7] = "共  " + pagecount + "  聯(lián)";
 
            excelapp.Visible = false;   //不顯示出來(lái)
            string strprintpath = ConfigurationManager.AppSettings["PrinterName"]; //打印機(jī)名稱 
            st1.PrintOut(Type.Missing, Type.Missing, Type.Missing, Type.Missing, strprintpath, Type.Missing, Type.Missing, Type.Missing);  //直接打印  
 
            book.Saved = true;
            excelapp.Workbooks.Close();
            excelapp.Visible = false;
            excelapp.Quit(); 
            GC.Collect();
            //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
        }
        catch (Exception ex)
        {
            strmes = ex.Message;
        } 
}
 
 
 
 

 


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 布尔津县| 夏邑县| 望都县| 福安市| 西盟| 南投县| 东光县| 金平| 台北市| 崇州市| 乐昌市| 治县。| 保山市| 宝兴县| 邵东县| 兰考县| 海阳市| 河西区| 黄平县| 宁安市| 嘉义县| 怀远县| 正镶白旗| 尖扎县| 原阳县| 东兴市| 兴宁市| 隆化县| 铁力市| 富锦市| 铁岭市| 界首市| 祥云县| 嵩明县| 新邵县| 临泉县| 蕉岭县| 洛浦县| 四子王旗| 石棉县| 平乐县|