/// <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="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)
{
try
{
int rowBase = 4,colBase = 1,rowIndex = rowBase,colIndex = colBase, nindex = 0;
decimal dfamount = 0.0M;
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);
}
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;
}
}