要输出芯片的检测人员姓名, 芯片型号,等数据到Excel 文件。 已整理为方法。
public void writeExcel(string date, string tester, string chipType,string chipNummberTotal, string chipFlawNummber)
{
//1.创建Applicaton对象
Microsoft.Office.Interop.Excel.Application xApp = new Microsoft.Office.Interop.Excel.Application();
//excel 写入后是否可视化
xApp.Visible = true;
string path = @"C:\Users\blyang\Desktop\chipWork.xlsx";
Microsoft.Office.Interop.Excel.Workbook xBook = xApp.Workbooks.Open(path, 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
Microsoft.Office.Interop.Excel.Worksheet xSheet = (Microsoft.Office.Interop.Excel.Worksheet)xBook.Sheets[1];
//获取最后一行,并在最新的一行输入。
int endRow = xSheet.UsedRange.CurrentRegion.Rows.Count + 1;
string excelRow = endRow.ToString();
//设置文件水平居中
xSheet.Cells.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;//水平居中
//设置获取 Excel部分区域并设置边框。
xSheet.Range[xSheet.Cells[1,1], xSheet.Cells[endRow, 5]].Cells.Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;//设置边框
//设置全部边框
//xSheet.Cells.Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;//设置边框
//4.写入数据
Microsoft.Office.Interop.Excel.Range carAmount = xSheet.get_Range("A"+ excelRow, Missing.Value);
carAmount.Value2 = date;
carAmount.Interior.ColorIndex = 2; //设备Range的背景色
Microsoft.Office.Interop.Excel.Range invalid_license_plate = xSheet.get_Range("B"+excelRow, Missing.Value);
invalid_license_plate.Value2 = tester;
invalid_license_plate.Interior.ColorIndex = 2; //设备Range的背景色
Microsoft.Office.Interop.Excel.Range chinese_character_wrong = xSheet.get_Range("C"+excelRow, Missing.Value);
chinese_character_wrong.Value2 = chipType;
chinese_character_wrong.Interior.ColorIndex = 2; //设备Range的背景色
Microsoft.Office.Interop.Excel.Range letter_wrong = xSheet.get_Range("D"+ excelRow, Missing.Value);
letter_wrong.Value2 = chipNummberTotal;
letter_wrong.Interior.ColorIndex = 2; //设备Range的背景色
Microsoft.Office.Interop.Excel.Range number_wrong = xSheet.get_Range("E"+excelRow, Missing.Value);
number_wrong.Value2 = excelRow;
number_wrong.Interior.ColorIndex = 2; //设备Range的背景色
Microsoft.Office.Interop.Excel.Range range = xSheet.Range[xSheet.Cells[1, 1], xSheet.Cells[5, 5]];
//range.Interior.ColorIndex = 3;
// xSheet.get_Range(xSheet.Cells[1, 1], xSheet.Cells[5, 5]).Cells.Font.Color = System.Drawing.ColorTranslator.ToOle(Color.FromArgb(000, 000, 255));//这里设置颜色,第一行的颜色,N列。
设置字体颜色
//xSheet.Range[xSheet.Cells[1, 1], xSheet.Cells[5, 5]].Cells.Font.Color = System.Drawing.ColorTranslator.ToOle(Color.FromArgb(000, 000, 255));//这里设置颜色,第一行的颜色,N列。
//设置背景颜色
xSheet.Range[xSheet.Cells[1, 1], xSheet.Cells[1, 5]].Cells.Interior.ColorIndex =6;
//xSheet.Range[xSheet.Cells[1, 1], xSheet.Cells[1, 5]].Cells.BackGround.Color = System.Drawing.ColorTranslator.ToOle(Color.FromArgb(000, 255, 255));//这里设置颜色,第一行的颜色,N列。
//5.保存保存WorkBook
xBook.Save();
if (!OpenExcel)
{
// 6.从内存中关闭Excel对象
xSheet = null;
xBook.Close();
xBook = null;
//关闭EXCEL的提示框
xApp.DisplayAlerts = true;
//Excel从内存中退出
xApp.Quit();
xApp = null;
}
}