//添加引用
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
方法一:將識別結(jié)果寫入到Excel表格中(根據(jù)指定模版寫入,并將其以當(dāng)前保存時間命名另存)
//**********************************************************/
方法二:將識別結(jié)果寫入到Excel表格中(在指定模版上直接寫入)
//1.創(chuàng)建Applicaton對象
Excel.Application xApp = new Excel.Application();
xApp.Visible = true;
//2.得到workbook對象,可以用兩種方式之一:下面是打開已有的文件
//Excel.Workbook xBook = xApp.Workbooks.Open(@"E:\5_調(diào)試軟件\ThinkCamWorkstation\PassData\192.168.116.248\2015-08-06\18\result.xlsx",
Excel.Workbook xBook = xApp.Workbooks.Open(path + "\\result.xlsx",
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value);
//3.指定要操作的Sheet
Excel.Worksheet xSheet = (Excel.Worksheet)xBook.Sheets[1];
//4.寫入數(shù)據(jù)
Excel.Range carAmount = xSheet.get_Range("J4", Missing.Value);
carAmount.Value2 = total;
carAmount.Interior.ColorIndex = 3; //設(shè)備Range的背景色
Excel.Range invalid_license_plate = xSheet.get_Range("M4", Missing.Value);
invalid_license_plate.Value2 = invalid;
invalid_license_plate.Interior.ColorIndex = 3; //設(shè)備Range的背景色
Excel.Range chinese_character_wrong = xSheet.get_Range("N4", Missing.Value);
chinese_character_wrong.Value2 = wrongCH;
chinese_character_wrong.Interior.ColorIndex = 3; //設(shè)備Range的背景色
Excel.Range letter_wrong = xSheet.get_Range("O4", Missing.Value);
letter_wrong.Value2 = wrongEN;
letter_wrong.Interior.ColorIndex = 3; //設(shè)備Range的背景色
Excel.Range number_wrong = xSheet.get_Range("P4", Missing.Value);
number_wrong.Value2 = wrongNU;
number_wrong.Interior.ColorIndex = 3; //設(shè)備Range的背景色
Excel.Range recognition_wrong = xSheet.get_Range("Q4", Missing.Value);
recognition_wrong.Value2 = wrongCL;
recognition_wrong.Interior.ColorIndex = 3; //設(shè)備Range的背景色
Excel.Range wrong_amount = xSheet.get_Range("T4", Missing.Value);
wrong_amount.Value2 = wrong;
wrong_amount.Interior.ColorIndex = 3; //設(shè)備Range的背景色
Excel.Range right_amount = xSheet.get_Range("U4", Missing.Value);
right_amount.Value2 = right;
right_amount.Interior.ColorIndex = 3; //設(shè)備Range的背景色
//5.保存保存WorkBook
xBook.Save();
//6.從內(nèi)存中關(guān)閉Excel對象
xSheet = null;
xBook = null;
xApp.Quit(); //這一句非常重要,否則Excel對象不能從內(nèi)存中退出
xApp = null;
MessageBox.Show(summary, "提示", MessageBoxButtons.OK);
聯(lián)系客服