/// <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;
}
}