print

Dim   xls       As   New   Excel.Application                 'EXCEL应用程序对象  
          Dim   xbook   As   Excel.Workbook                     'EXCEL工作薄对象  
          Dim   xsheet   As   Excel.Worksheet             'EXCEL工作表对象  
          Set   xbook   =   xls.Workbooks.Add  
   
          Set   xsheet   =   xbook.Worksheets(1)  
          '对xsheet操作,填充单元格  
          '……  
   
  With   xsheet.PageSetup  
            .PaperSize   =   xlPaperA4'A4纸  
            .Orientation   =   xlLandscape'横向  
            .CenterHeader   =   "&""宋体,常规""&14表头"  
            .CenterFooter   =   "&""宋体,常规""&P"  
  End   With  
  xls.Visible   =   True'显示Excel  
  xsheet.PrintPreview'打印预览   

//////////////////////////////////////////////////////////////////////////////////////////////////////

#region 声明
using System;
using System.Runtime.InteropServices;
using System.IO;
#endregion
namespace LongRuan
{
/// <summary>
/// POSPrinter 的摘要说明。
/// </summary>
public class POSPrinter
{
const int OPEN_EXISTING = 3;
string prnPort ="LPT1";
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
private static extern IntPtr CreateFile(string lpFileName,
int dwDesiredAccess, 
int dwShareMode, 
int lpSecurityAttributes,
int dwCreationDisposition ,
int dwFlagsAndAttributes ,
int hTemplateFile);
public POSPrinter()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public POSPrinter(string prnPort)
{
this.prnPort=prnPort;//打印机端口
}
public  string PrintLine(string str)
{
IntPtr iHandle = CreateFile(prnPort,  0x40000000, 0, 0, OPEN_EXISTING, 0, 0);
if(iHandle.ToInt32() == -1)
{
return "没有连接打印机或者打印机端口不是LPT1";
}
else
{
FileStream fs = new FileStream(iHandle, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default); //写数据
sw.WriteLine(str);
//开钱箱
//sw.WriteLine(Chr(&H1B) & Chr(70) & Chr(0) & Chr(20) & Chr(&HA0))
sw.Close();
fs.Close();
return "";
}
}
}
}


调用方法:
LongRuan.POSPrinter prn = new LongRuan.POSPrinter("LPT1");
string str =prn.PrintLine("写端口测试!");
if(str !="")
MessageBox.Show(str);

 

///////////////////////////////////////

using System;
using System.IO; 
using System.Drawing.Printing; 
using System.Runtime.InteropServices; 


public class RawPrinterHelper 

    [StructLayout(LayoutKind.Sequential, CharSet
=CharSet.Unicode)] 
        
public struct DOCINFOW 
    

        [MarshalAs(UnmanagedType.LPWStr)] 
        
public string pDocName; 
        [MarshalAs(UnmanagedType.LPWStr)] 
        
public string pOutputFile; 
        [MarshalAs(UnmanagedType.LPWStr)] 
        
public string pDataType; 
    }
 

    [DllImport(
"winspool.Drv", EntryPoint="OpenPrinterW", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode, SetLastError=true, ExactSpelling=true)]
    
public static extern bool OpenPrinter(string src, ref IntPtr hPrinter, long pd);
 


    [DllImport(
"winspool.Drv", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode, SetLastError=true, ExactSpelling=true)]
    
public static extern bool ClosePrinter(IntPtr hPrinter);
 


    [DllImport(
"winspool.Drv", EntryPoint="StartDocPrinterW", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode, SetLastError=true, ExactSpelling=true)]
    
public static extern bool StartDocPrinter(IntPtr hPrinter, int level, ref RawPrinterHelper.DOCINFOW pDI);
 


    [DllImport(
"winspool.Drv", EntryPoint="EndDocPrinter", SetLastError=true, CharSet=CharSet.Unicode, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)] 
    
public static extern bool EndDocPrinter(IntPtr hPrinter); 
    

    [DllImport(
"winspool.Drv", EntryPoint="StartPagePrinter", SetLastError=true, CharSet=CharSet.Unicode, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)] 
    
public static extern bool StartPagePrinter(IntPtr hPrinter) ;
    

    [DllImport(
"winspool.Drv", EntryPoint="EndPagePrinter", SetLastError=true, CharSet=CharSet.Unicode, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)] 
    
public static extern bool EndPagePrinter(IntPtr hPrinter) ;
    

    [DllImport(
"winspool.Drv", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode, SetLastError=true, ExactSpelling=true)]
    
public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, int dwCount, ref int dwWritten);
 


    
public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount) 
    

        IntPtr hPrinter 
=  System.IntPtr.Zero; 
        Int32 dwError; 
        DOCINFOW di 
= new DOCINFOW(); 
        Int32 dwWritten 
= 0
        
bool bSuccess; 
        di.pDocName 
= "My Document"
        di.pDataType 
= "RAW"
        bSuccess 
= false
        
if (OpenPrinter(szPrinterName,ref hPrinter, 0)) 
        

            
if (StartDocPrinter(hPrinter, 1,ref di)) 
            

                
if (StartPagePrinter(hPrinter)) 
                

                    bSuccess 
= WritePrinter(hPrinter, pBytes, dwCount, ref dwWritten); 
                    EndPagePrinter(hPrinter); 
                }
 
                EndDocPrinter(hPrinter); 
            }
 
            ClosePrinter(hPrinter); 
        }
 
        
if (bSuccess == false
        

            dwError 
= Marshal.GetLastWin32Error(); 
        }
 
        
return bSuccess; 

    }
 


    
public static bool SendFileToPrinter(string szPrinterName, string szFileName) 
    

        FileStream stream1 
= new FileStream(szFileName, FileMode.Open);
        BinaryReader reader1 
= new BinaryReader(stream1);
        
byte[] buffer1 = new byte[((int) stream1.Length) + 1];
        buffer1 
= reader1.ReadBytes((int) stream1.Length);
        IntPtr ptr1 
= Marshal.AllocCoTaskMem((int) stream1.Length);
        Marshal.Copy(buffer1, 
0, ptr1, (int) stream1.Length);
        
bool flag1 = RawPrinterHelper.SendBytesToPrinter(szPrinterName, ptr1, (int) stream1.Length);
        Marshal.FreeCoTaskMem(ptr1);
        
return flag1;

    }
 

    
public static void SendStringToPrinter(string szPrinterName, string szString) 
    

        IntPtr pBytes; 
        Int32 dwCount; 
        dwCount 
= szString.Length; 
        pBytes 
= Marshal.StringToCoTaskMemAnsi(szString); 
        SendBytesToPrinter(szPrinterName, pBytes, dwCount); 
        Marshal.FreeCoTaskMem(pBytes); 
    }
 
}



调用

 


    sPrintStr   
= " test"

    PrintDialog pd 
= new PrintDialog(); 
    pd.PrinterSettings 
= new PrinterSettings(); 
    
//if (pd.ShowDialog(this) > DialogResult.None)
    
//{
    RawPrinterHelper.SendStringToPrinter(pd.PrinterSettings.PrinterName, sPrintStr); 
    
//}


     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值