Windows · 2010-05-25

C#用 excel 作为模板打印的源码

源码

/*
‘   //-*****************************************************-//
‘  //            如果您要献一份爱心,请参预希望工程!       //
‘ //                如果您还支持作者,请联系作者!         //
‘//-*****************************************************-//

‘    /// 作 者:长江支流(周方勇)
‘    /// Email:[email protected]  QQ:150439795
‘    /// 网 址:www.webmis.com.cn
‘    /// ★★★★★您可以免费使用此程序,但是请您完整保留此说明,以维护知识产权★★★★★

*/

//打印操作,套打、打印、预览
enum PrintFlag
{
/// <summary>
/// 套打,只打印没有印刷的部分
/// </summary>
CasePrint,
/// <summary>
/// 打印全部
/// </summary>
PrintAll,
/// <summary>
/// 预览全部
/// </summary>
PreviewAll
}

//套打、打印、预览三个按钮关联些委托实例
private void Print_Click(object sender, System.EventArgs e)
{
Button btn = (Button)sender;
switch(btn.Tag.ToString())
{
case “套打”:
Print(PrintFlag.CasePrint);
break;
case “打印”:
Print(PrintFlag.PrintAll);
break;
case “预览”:
Print(PrintFlag.PreviewAll);
break;
}
}

private void Print(PrintFlag p_printFlag)
{

//制作步骤:
/*    1、用Excel作出与要打印的样式一样的电子表格存为模板;
*     技巧:最好把第一行与第一列作为空行,以利于调整边距(虽然Excel、打印机可调整页边距), 尽量的在需要调整的地方多空几行与几列,以利于调整套打对准
*
*  2、如同本程序一样,将Excel作为套打的模板,直接将要打印的数据写入;
*
*  3、打印,根据实际的效果调整Excel模板行高列宽及空出的行列, 直到能够准确的套上。将模板拷贝一份,清除模板上的文字与网格线,做成套打的模板。
*/

#region 套打、打印预览

//用Excel打印,步骤为:打开、写数据、打印预览、关闭
GoldPrinter.ExcelExpert.ExcelBase excel = new GoldPrinter.ExcelExpert.ExcelBase();
string strFileName = “invoice.xlt”;            //模板文件名

if (p_printFlag == PrintFlag.CasePrint)
{
strFileName = “invoiceCase.xlt”;        //套打模板文件名
}

string strExcelTemplateFile = System.IO.Path.GetFullPath(@”….ExcelTemplate” + strFileName);

excel.Open(strExcelTemplateFile);                            // 用模板文件
excel.Visible = false;                                        //建议:如果excel不可见且在编程情况下写数据特 别是大量数据时
excel.ScreenUpdating = false;                                //设置此开关能大大提高效率。写完后如要可见,再设置此属性为真刷新屏 幕。
excel.Caption = “税 务 机 关 代 开 统 一 发 票(国 税)”;    //”MIS金质打印通  通打天下报表”;

//在模板中写入要打印的数据

//***发票抬头***

//年月日
excel.SetCellText(7,”B”,txtYear.Text + “年” + txtMonth.Text + “月” + txtDay.Text + “日” );

//付款方名称
excel.SetCellText(8,”D”,txtPayer.Text);
//收款方名称
excel.SetCellText(9,”D”,txtCollecter.Text);
//及地址、电话
excel.SetCellText(11,”D”,txtCollecterAddTel.Text);

// 代开普通发票    申 请 表 号 码
excel.SetCellText(8,”J”,txtInvoiceApplicationNo.Text);
//收款方识别号或 证 件 号 码
excel.SetCellText(9,”J”,txtCollecterID.Text);

//***品名及金额、备注***
//B14到B23是品名   F14到F23为金额

excel.SetCellText(“B14”,txtP1.Text);
excel.SetCellText(“F14”,txtJ1.Text);

excel.SetCellText(“B15”,txtP2.Text);
excel.SetCellText(“F15”,txtJ2.Text);

excel.SetCellText(“B16”,txtP3.Text);
excel.SetCellText(“F16”,txtJ3.Text);

excel.SetCellText(“B17”,txtP4.Text);
excel.SetCellText(“F17”,txtJ4.Text);

excel.SetCellText(“B18”,txtP5.Text);
excel.SetCellText(“F18”,txtJ5.Text);

excel.SetCellText(“B19”,txtP6.Text);
excel.SetCellText(“F19”,txtJ6.Text);

excel.SetCellText(“B20”,txtP7.Text);
excel.SetCellText(“F20”,txtJ7.Text);

excel.SetCellText(“B21”,txtP8.Text);
excel.SetCellText(“F21”,txtJ8.Text);

excel.SetCellText(“B22”,txtP9.Text);
excel.SetCellText(“F22”,txtJ9.Text);

excel.SetCellText(“B23”,txtP10.Text);
excel.SetCellText(“F23″,txtJ10.Text);

//备注
//excel.SetCellText(14,”I”,txtMemo.Imag.);

//***发票总金额***

//合计人民币   (大写)
excel.SetCellText(24,”D”,txtTotalUpper.Text);
//合计人民币   (小写)
excel.SetCellText(24,”K”,txtTotalLower.Text);

//税额   (大写)
excel.SetCellText(25,”D”,txtTaxUpper.Text);
//税额   (小写)
excel.SetCellText(25,”L”,txtTaxLower.Text);

//***发票尾***
//税控码
excel.SetCellText(26,”C”,txtTaxControlCode.Text);
//开票人:
excel.SetCellText(26,”H”,txtWriter.Text);

//刷新Excel屏幕
excel.ScreenUpdating = true;

if (p_printFlag == PrintFlag.CasePrint || p_printFlag == PrintFlag.PrintAll)
{
excel.Print();                //打印
}
else
{
excel.PrintPreview();        //预览
}

excel.Close();                    //关闭并释放

#endregion

}

private void frmInvoice_Load(object sender, System.EventArgs e)
{
//初始当日日期
System.DateTime dt = System.DateTime.Now;
SetToday(dt);
}

private void btnExit_Click(object sender, System.EventArgs e)
{
this.Close();
}

//回车
private void frmInvoice_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
SendKeys.Send(“{TAB}”);
}
}

//金额小写转人民币大写
private void txtTotalLower_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
SetUpperMoney();
}

//重新总计
private void txtJX_TextChanged(object sender, System.EventArgs e)
{
double dblMoney = 0;

dblMoney += GetInputMoney(txtJ1.Text);
dblMoney += GetInputMoney(txtJ2.Text);
dblMoney += GetInputMoney(txtJ3.Text);
dblMoney += GetInputMoney(txtJ4.Text);
dblMoney += GetInputMoney(txtJ5.Text);
dblMoney += GetInputMoney(txtJ6.Text);
dblMoney += GetInputMoney(txtJ7.Text);
dblMoney += GetInputMoney(txtJ8.Text);
dblMoney += GetInputMoney(txtJ9.Text);
dblMoney += GetInputMoney(txtJ10.Text);

txtTotalLower.Text = dblMoney.ToString();
SetUpperMoney();

}

//改变税率重算
private void cboTaxRate_TextChanged(object sender, System.EventArgs e)
{
SetUpperMoney();
}

private void btnRefDate_Click(object sender, System.EventArgs e)
{
cldSelect.Visible = true;
cldSelect.SetDate(new DateTime(int.Parse(txtYear.Text),int.Parse(txtMonth.Text),int.Parse(txtDay.Text)));
cldSelect.Focus();
}

private void cldSelect_DateSelected(object sender, System.Windows.Forms.DateRangeEventArgs e)
{
SetToday(e.End);
cldSelect.Visible = false;
}

//大写合计人民币、税额
private void SetUpperMoney()
{
try
{
//                string strUpper = GoldPrinter.ExcelExpert.ChineseNum.GetUpperMoney(Double.Parse(txtTotalLower.Text));
//                //合计人民币
//                txtTotalUpper.Text = strUpper;
//
//                strUpper = GoldPrinter.ExcelExpert.ChineseNum.GetUpperMoney(Double.Parse(txtTotalLower.Text) * Double.Parse(cboTaxRate.Text) / 100);
//                //税额 = 合计人民币 * 税率
//                txtTaxUpper.Text = strUpper;

}
catch{}
}

private double GetInputMoney(string p_text)
{
double dblReturn = 0;
try
{
dblReturn = double.Parse(p_text);
}
catch{}

return dblReturn;
}

private void SetToday(System.DateTime dt)
{
txtYear.Text = dt.Year.ToString();

txtMonth.Text = GetLengthTwoDate(dt.Month.ToString());
txtDay.Text = GetLengthTwoDate(dt.Day.ToString());
}

private string GetLengthTwoDate(string p_MonthOrDay)
{
string strReturn = p_MonthOrDay;
if (strReturn.Length == 1)
{
strReturn = “0” + strReturn;
}

return strReturn;
}

}//End Class
}//End Namespace