首页 > 代码库 > C#实现对EXCEL指定单元格进行操作
C#实现对EXCEL指定单元格进行操作
- using System;
- using System.Collections.Generic;
- using System.Text;
- //先添加引用
- using Microsoft.Office.Interop.Excel;
- using System.IO;
- using System.Reflection;
- namespace SighExcel
- {
- public class Sign
- {
- /// <summary>
- /// 对Excel指定单元格进行操作
- /// </summary>
- /// <param name="filePath">EXCEL表格所′在路径</param>
- /// <param name="row">行号</param>
- /// <param name="column">列号</param>
- /// <param name="code">内容</param>
- /// <returns></returns>
- public bool signExcel(string filePath,int row, int column,string code)
- {
- try
- {
- _Application excelApp = new Application();
- Workbook wBook = excelApp.Workbooks.Open(filePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
- Worksheet wSheet = (Worksheet)wBook.Worksheets[1];
- wSheet.Cells[row, column] = code;
- Microsoft.Office.Interop.Excel.Range rtemp = wSheet.Range[wSheet.Cells[3, 4], wSheet.Cells[3, 6]];
- rtemp.Font.Name="宋体";
- rtemp.Font.Size=12;
- object SaveChanges = XlSaveAction.xlDoNotSaveChanges;
- wBook.Save();
- wBook.Close();
- excelApp.Quit();
- return true;
- }
- catch (Exception e)
- {
- System.IO.File.AppendAllText(System.IO.Path.Combine(GetCurrentPath(), "signExcellog.txt"), "文件" + filePath + "签入编号失败!错误:" + e.Message.ToString() + "。" + DateTime.Now.ToString("yyyy-MM-dd-hh:mm:ss"));
- return false;
- }
- }
- /// <summary>
- ///得到当前程序的路径
- /// </summary>
- /// <returns></returns>
- static public string GetCurrentPath()
- {
- string asstring = Assembly.GetExecutingAssembly().Location;
- string[] aa = asstring.Split(‘\\‘);
- string path = string.Empty;
- foreach (string var in aa)
- {
- if (var != aa[aa.Length - 1])
- path += var + @"\";
- }
- return path;
- }
- }
- }
C#实现对EXCEL指定单元格进行操作
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。