• C#操作Excel基本操作


    ///
    using Microsoft.Office.Core;
    using Microsoft.Office.Interop.Excel;
    using System.IO;
    using System.Reflection;
    
    <summary> /// 导出按钮的点击事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); try { excel.SheetsInNewWorkbook = 1; excel.Workbooks.Add(); //获取第一个工作表 Worksheet wsheet = (Worksheet)excel.ActiveWorkbook.Worksheets[1]; wsheet.Name = "顾客信息表"; //设置excel列名 excel.Cells[1, 1] = "客户姓名"; excel.Cells[1, 2] = "身份证号"; excel.Cells[1, 3] = "入住日期"; excel.Cells[1, 4] = "订金"; excel.Cells[1, 5] = "状态"; excel.Cells[1, 6] = "房间编号"; //获取标题行的单元格 即Range var TitilRow = excel.Range[excel.Cells[1,1],excel.Cells[1,7]] as Range; //设置字体加粗 TitilRow.Font.Bold = true; //设置字体颜色 TitilRow.Font.ColorIndex = 0; //设置背景颜色 TitilRow.Interior.ColorIndex = 15; //设置边框样式 TitilRow.Borders.LineStyle = XlLineStyle.xlContinuous; //设置单元格的宽度 TitilRow.ColumnWidth = 18; int i, j = 0; for (i = 0; i < dgvGuestInfo.Rows.Count; i++) { //循环将DataGridView的数据放到 Excel 文档中 for (j = 0; j < 6; j++) { excel.Cells[i + 2, j + 1] = dgvGuestInfo.Rows[i].Cells[j].Value.ToString(); } } ////设置出生年月日的格式 //excel.get_Range(excel.Cells[2, 8], excel.Cells[i + 2, 8]).NumberFormat = "yyyy-MM-dd"; //设置身份证号的个格式 var Identity = excel.Range[excel.Cells[2, 2], excel.Cells[i, 2]].NumberFormatLocal = "0"; //获取活动的行和列 var ActiveRowAndColums = excel.Range[excel.Cells[1, 7], excel.Cells[i, 7]] as Range; //设置活动的行和列居中显示 ActiveRowAndColums.VerticalAlignment = XlVAlign.xlVAlignCenter; //显示Excel文件内容 excel.Visible = true; //设置显示的时长 System.Threading.Thread.Sleep(5000); //设置保存的文件的名称 excel.ActiveWorkbook.SaveAs(Environment.CurrentDirectory + "/顾客信息表.xls", XlFileFormat.xlWorkbookNormal); } catch (Exception ex) { throw ex; } finally { //关闭当前活动的WorkBook excel.ActiveWorkbook.Close(); //退出excel应用程序 excel.Quit(); } } 类似这样写,应该能行 需要注意的是finally里面的代码!

    来自于

  • 相关阅读:
    基于HTML5技术的电力3D监控应用(三)
    XCODE 出现 The operation couldn't be completed.(LaunchServicesError error 0.)错误修复
    iOS本地动态验证码生成-b
    裁剪出环形图片
    UITableView实现格瓦拉飞天投票模块-b
    高仿猫眼电影选座(选票)模块-b
    java和php实现RSA加密互通-b
    iOS10 权限崩溃问题-b
    iOS百度地图路径规划和POI检索详细总结-b
    iOS 的 Gif 渲染
  • 原文地址:https://www.cnblogs.com/AiYaTou/p/5062919.html
Copyright © 2020-2023  润新知