标签:c# excel
关于Excel在软件系统中的作用 , 如果是大型项目的话 , 一般会用Excel做配置 . 游戏公司尤其是偏好这个. 想想看 , 要策划人员去写XML 或者 JSON 配置文件 , 这个我觉得有点为难策划 . 而且容易配置错误. 如果让他们利用Excel呢 ? 如果你觉得不安全 , 完全可以将Excel导出 , 保存为二进制文件 , 供程序员调用 .
1, 需要应用 : using Microsoft.Office.Interop.Excel;
一 : 新建一个Excel
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Office.Interop.Excel; namespace MyExcel { class Program { static void Main(string[] args) { Application excel = new Application(); //设置2个工作表 excel.SheetsInNewWorkbook = 2; //添加一个工作簿 excel.Workbooks.Add(); //获得第一个工作表 Worksheet sheet1 = (Worksheet)excel.ActiveWorkbook.Worksheets[1]; sheet1.Name = "Xiaoxue"; sheet1.Cells[1,1] = "Eisa"; sheet1.Cells[3, 1] = "1222211111"; sheet1.Cells[2, 2] = new DateTime(); // 样式设置 Range range = excel.Range[excel.Cells[1, 1], excel.Cells[1, 9]]; range.Font.Bold = true; range.Font.ColorIndex = 4; range.Interior.ColorIndex = 1; range.Borders.LineStyle = XlLineStyle.xlDash; excel.Range[excel.Cells[2, 1], excel.Cells[9, 1]].NumberFormatLocal = "@"; // 电话好码 excel.Range[excel.Cells[2, 2], excel.Cells[9, 2]].NumberFormat = "yyyy-m-d";//日期显示格式 excel.Visible = true; } } }
看看结果:
现在 , 去选择一个目标Excel , 并且把其数据读出来
①:得到Excel文件路径
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; namespace ExcelTest.com { /// <summary> /// 返回一个Excel文件的路径 /// </summary> public class ExcelPath { private string basePath; /// <summary> /// /// </summary> /// <param name="basePath"> 初始路径 如 : "C:\\" </param> public ExcelPath(string basePath) { this.basePath = basePath; } public string getExcelFileName() { if (this.basePath != "") { // 需要引用 : System.Windows.Forms OpenFileDialog excelDialog = new OpenFileDialog(); excelDialog.InitialDirectory = this.basePath; excelDialog.Filter = "Microsoft Excel files (*.xls)|*.xlsx"; if (excelDialog.ShowDialog() == DialogResult.OK) { if ((excelDialog.OpenFile()) != null) { return excelDialog.FileName; } } return string.Empty; } else { return string.Empty; } } } }
②:核心读取目标Excel的数据
关于如何关闭Excel的进程 , 需要引用 : System.Runtime.InteropServices;
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using ExcelTest.com; using Microsoft.Office.Interop.Excel; using System.Runtime.InteropServices; namespace ExcelTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); //getExcelPath(); handlerExcelFile("Sheet1"); } /// <summary> /// 获得目标Excel路径 /// </summary> /// <returns></returns> private string getExcelPath() { // 初始化实例 ExcelPath openExcel = new ExcelPath( "e:\\" ); string excelPath = openExcel.getExcelFileName(); Console.WriteLine("我得到的Excel文件路径{0}", excelPath); return excelPath; } /// <summary> /// 获得Excel应用实例 /// </summary> private Microsoft.Office.Interop.Excel.Application getExcelApp() { //创建Excel Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); // 打开选择的excel文件 excel.Workbooks.Open( getExcelPath() ); return excel; } /// <summary> /// /// </summary> /// <param name="sheetName"> 工作表的名称 </param> private void handlerExcelFile( string sheetName ) { Microsoft.Office.Interop.Excel.Application excel = getExcelApp(); // 定义工作表 Worksheet sheet = null; //查找目标工作表 foreach (Worksheet wsheet in excel.ActiveWorkbook.Sheets) { if (sheetName == wsheet.Name) { sheet = wsheet; break; } } if (sheet != null) { Console.WriteLine("测试数据 : {0}", Convert.ToString( (sheet.Cells[1, 1] as Range).Text )); } //关闭进程 Marshal.ReleaseComObject(sheet); Marshal.ReleaseComObject(excel); sheet = null; excel = null; GC.Collect(); } } }
结果 测试 :
本文出自 “Better_Power_Wisdom” 博客,请务必保留此出处http://aonaufly.blog.51cto.com/3554853/1626231
标签:c# excel
原文地址:http://aonaufly.blog.51cto.com/3554853/1626231