C# excel 文件操作代码合集整理

   要输出芯片的检测人员姓名, 芯片型号,等数据到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;
            }


        }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值