初次写博客,有哪些不足的地方,还请多多指点,给予建议,谢谢!
如若想要源码,请留言。
本实例是在Webservice 中通过excel做模板来打印标签。
具体需求是:一个订单一页纸打印4行分录,如果超过4行,则再次按照原格式换纸打印,如果行数未满4行,则补空行。
一、实现步骤:
1、首先在EXCEL 画好模版 具体需求是:一个订单一页纸打印4行分录,如果超过4行,则再次按照原格式换纸打印,如果行数未满4行,则补空行。
一、实现步骤:
2、在程序中调用EXCEL 填充数据
3、调用EXCEL打印方法打印
二、源码重点讲解:
1、List<> 泛型集合保存打印的数据,通过它可以删除已经打印过的数据
示例:
for (int i = 0; i <= list.Count; i++)
for (int i = 0; i <= list.Count; i++)
{
if (i > 0) i--; //每当执行完下一次的时候,将这个值初始化为0,即i--
if (nindex == Convert.ToInt32(pagesize)) break; //如果超过指定的行数,则跳出循环
FitemData model = list[i];
..........
list.Remove(model); //移除当前行
}
2、While 循环:当条件为真的时候执行循环体,先判断后执行,循环次数不固定
示例:
==================================源码讲解===========================..........
list.Remove(model); //移除当前行
}
2、While 循环:当条件为真的时候执行循环体,先判断后执行,循环次数不固定
示例:
while (list.Count > 0)
{
{
PrintExccel(ref list, damountcount.ToString(), FirstPageText.ToString(), PageCount.ToString(), PageSize.ToString(), ref strmes);
FirstPageText++;
} /// <summary>
/// 打印方法
/// </summary>
/// <param name="dt">打印的数据</param>
/// <param name="strmes">bug返回的异常信息</param>
private void Print(DataTable dt, ref string strmes) {
List<FitemData> list = new List<FitemData>();
decimal damountcount = 0.00M; //合计小写金额
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"]); //每页行数
int PageCount = 0; //总页数
int FirstPageText = 1; //首页
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">总记录数</param>
/// <param name="list">总记录数</param>
/// <param name="damountcount">小写金额总计</param>
/// <param name="firsrpagetext">页码数</param>
/// <param name="pagesize">每页行数</param>
/// <param name="strmes">错误参数回写</param>
private void PrintExccel(ref List<FitemData> list, string damountcount, string firsrpagetext, string pagecount, string pagesize, ref string strmes)
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]; // 选择的工作表,序号默认是从1开始即(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; //如果超过指定的行数,则跳出循环
FitemData model = list[i];
//复制格式 插入一个新行
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);
list.Remove(model);
}
if (nindex < Convert.ToInt32(pagesize))
{
for (int i = 0; i < Convert.ToInt32(pagesize) - nindex; i++)
{
//复制格式 插入一个新行
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); //删除多余行 (即固定订单分录行数下多出的一行)
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 + " 联";
st1.Cells[rowIndex + 2, 7] = "共 " + pagecount + " 联";
excelapp.Visible = false; //不显示出来
string strprintpath = ConfigurationManager.AppSettings["PrinterName"]; //打印机名称
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;
}
}
}