工具:UniExcel插件Unity-Unity3D文档类资源-CSDN下载
文件路径:string outPutDir = Application.dataPath + "\\" + "MyExcel.xls";
1.1(创建/修改)没有打开的表格:
using (ExcelPackage package = new ExcelPackage(new FileInfo(Application.dataPath + "\\" + "MyExcel.xls")))
{
worksheet = package.Workbook.Worksheets.Add("我的Excel");
worksheet.Cells[1, 1].Value = "1A";
worksheet.Cells[1, 2].Value = "1B";
worksheet.Cells[1, 3].Value = "1C";
package.Save();
}
1.2(读)可以是已经打开的表格:
FileStream fs = File.Open(readPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
IExcelDataReader excelRead = ExcelReaderFactory.CreateOpenXmlReader(fs);
DataSet result = excelRead.AsDataSet();
//行 Tables[0]表示的是表1
int row = result.Tables[0].Rows.Count;
//列
int columns = result.Tables[0].Columns.Count;
ps:表格索引也可以用string表示:worksheet.Cells[A1].Value = "1A";
2.(合并/拆分)单元格:
using (ExcelPackage package = new ExcelPackage(new FileInfo(Application.dataPath + "\\" + "MyExcel.xls")))
{
worksheet = package.Workbook.Worksheets[1];
worksheet.Cells[1, 1, 3, 3].Merge = true;//合并
worksheet.Cells[1, 1, 3, 3].Merge = false;//拆分
package.Save();
}
ps:合并之后表格显示为索引值最小的表格的值,但是被合并表格的值依然存在,也可通过代码操作
3.设置 字体,字号,字色:
//设置样式大小和颜色
worksheet.Cells["F1"].Value = "123";
worksheet.Cells["F1"].Style.Font.Size = 20;
worksheet.Cells["F2"].Style.Font.Name = "宋体";
worksheet.Cells["F1"].Style.Border.BorderAround(OfficeOpenXml.Style.ExcelBorderStyle.Thin);
4.
|