标签:epp mda com creat inf ack new str omd
1.输入授权
//输入授权码 ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
2.写入excel
//写入excel DataTable dt = new DataTable("测试表"); dt.Columns.Add(new DataColumn("ID")); dt.Columns.Add(new DataColumn("Name")); for (int i = 0; i < 10; i++) { var dr = dt.NewRow(); dr[0] = i; dr[1] = "第" + i + "个名称"; dt.Rows.Add(dr); } using (ExcelPackage package = new ExcelPackage()) { ExcelWorksheet sheet = package.Workbook.Worksheets.Add(dt.TableName); sheet.Cells.LoadFromDataTable(dt); sheet.Cells["C1"].Value = "我是C1"; using (Stream stream = new FileStream("D:\\123.xlsx", FileMode.Create)) { package.SaveAs(stream); } }
3.读取excel
//读取excel using (var package = new ExcelPackage(new FileInfo("D:\\123.xlsx"))) { var sheet = package.Workbook.Worksheets[0]; var colCount = sheet.Dimension.End.Column; var rowCount = sheet.Dimension.End.Row; for (int r = sheet.Dimension.Start.Row; r < rowCount; r++) { string rowInfo = ""; for (int c = sheet.Dimension.Start.Column; c <= colCount; c++) { var value = sheet.GetValue<string>(r, c); rowInfo = rowInfo + " "+value; } Console.WriteLine("数据:"+rowInfo); } }
标签:epp mda com creat inf ack new str omd
原文地址:https://www.cnblogs.com/lizhijian/p/14046667.html