C#读写Excel实践笔记
2024-09-22 05:44:14
使用第三类包:NPOI
介绍
Github地址:https://github.com/tonyqus/npoi,Java POI项目的.NET版。
通过它可以在没有安装Office软件的情况下,快速的读写Office文档。
特点:
- 跨平台
- 支持Office2003及以后所有版本
- 支持Excel的大部分特性
- 开源
- 实例丰富
- 维护者是中国人,所以交流应该问题不大
Nuget包下载:https://www.nuget.org/packages/NPOI/
实践一:读取Excel数据
注意:行列开始索引为0,没有数据的Cell为NULL。
/// <summary>
/// 读取Excel所有单元格数据
/// </summary>
/// <param name="path">文件路径</param>
/// <param name="sheetName">Sheet名</param>
/// <param name="startRow">读取开始行位置</param>
/// <param name="columns">读取列表</param>
/// <returns>单元格列表</returns>
public static async Task<IList<ICell>> ReadAllCellsAsync(string path, string sheetName, int startRow = , IList<int> columns = null)
{
var ret = new List<ICell>(); await Task.Factory.StartNew(() =>
{
using (var file = new FileStream(path, FileMode.Open, FileAccess.Read))
{
var book = WorkbookFactory.Create(file);
var sheet = book?.GetSheet(sheetName);
if (sheet != null)
{
for (int row = startRow - ; row <= sheet.LastRowNum; row++)
{
var rowValue = sheet.GetRow(row);
if (rowValue == null)
{
continue;
} if (columns == null || columns?.Count <= )
{
columns = Enumerable.Range(, rowValue.LastCellNum + ).ToList();
} foreach (int col in columns)
{
var cell = rowValue.GetCell(col - );
if (cell == null)
{
continue;
} ret.Add(cell);
}
}
}
book?.Close();
}
}); return ret;
}
取Cell值扩展函数
public static string GetCellValue(this ICell cell)
{
if (cell == null)
{
return string.Empty;
} switch (cell.CellType)
{
case CellType.Formula:
case CellType.String:
return cell.StringCellValue;
case CellType.Numeric:
if (DateUtil.IsCellDateFormatted(cell))
{
return cell.DateCellValue.ToString();
}
else
{
return cell.NumericCellValue.ToString();
}
case CellType.Boolean:
return cell.BooleanCellValue.ToString();
default:
return string.Empty;
}
}
实践二:写Excel数据
1,创建Book。
public static IWorkbook CreateBook(this string path)
{
IWorkbook book;
string extension = Path.GetExtension(path); // HSSF => Microsoft Excel(xls形式)(excel 97-2003)
// XSSF => Office Open XML Workbook形式(xlsx形式)(excel 2007+)
if (extension == ".xls")
{
book = new HSSFWorkbook();
}
else
{
book = new XSSFWorkbook();
} return book;
}
2,创建Sheet。
var sheet = book.CreateSheet(“test”);
3,创建单元格。(为了不区分HSSHyperLink与XSSFHyperLink直接使用了设置函数形式)
ICellStyle hlink_style = book.CreateCellStyle();
IFont hlink_font = book.CreateFont();
hlink_font.Underline = FontUnderlineType.Single;
hlink_font.Color = IndexedColors.Blue.Index;
hlink_style.SetFont(hlink_font); var row = sheet.GetRow(rowIndex) ?? sheet.CreateRow(rowIndex);
var cell = row.GetCell(columnIndex) ?? row.CreateCell(columnIndex); cell.SetCellValue("test"); // OR hyperlink
var linkcell = row.GetCell(columnIndex) ?? row.CreateCell(columnIndex);
linkcell.SetCellFormula($"hyperlink(\"{linkValue}\",\"{value}\")");
linkcell.CellStyle = hlink_style;
4,创建合并单元格。
sheet.AddMergedRegion(new CellRangeAddress(rowStart, rowEnd, columnStart, columnEnd));
5,创建单元格数据入力规则。
public static void WriteValidation(this ISheet sheet, int rowStart, int rowEnd, int columnStart, int columnEnd, string[] values)
{
var addressList = new CellRangeAddressList(rowStart, rowEnd, columnStart, columnEnd);
var helper = sheet.GetDataValidationHelper();
var dataValidation = helper.CreateValidation(helper.CreateExplicitListConstraint(values), addressList); if (dataValidation is XSSFDataValidation)
{
dataValidation.SuppressDropDownArrow = true;
dataValidation.ShowErrorBox = true;
}
else
{
dataValidation.SuppressDropDownArrow = false;
}
dataValidation.EmptyCellAllowed = true;
dataValidation.ShowPromptBox = true;
sheet.AddValidationData(dataValidation);
}
6,创建过滤器。
sheet.SetAutoFilter(new CellRangeAddress(row + , row + , , col - ));
7,创建自动调整宽度。
for (int i = ; i <= col; i++)
{
sheet.AutoSizeColumn(i, true);
}
8,单元格格式设置。
注意:如果所有单元格使用同一个ICellStyle实例,那CellStyle对象一样。
var commonFont = book.CreateFont();
commonFont.Color = IndexedColors.Black.Index;
commonFont.FontHeightInPoints = ;
var Common= book.CreateCellStyle();
Common.SetFont(commonFont);
Common.BorderBottom = BorderStyle.Thin;
Common.BorderLeft = BorderStyle.Thin;
Common.BorderRight = BorderStyle.Thin;
Common.BorderTop = BorderStyle.Thin;
Common.WrapText = true;
Common.Alignment = HorizontalAlignment.Left;
Common.VerticalAlignment = VerticalAlignment.Center; var row = sheet.GetRow(rowIndex) ?? sheet.CreateRow(rowIndex);
var cell = row.GetCell(columnIndex) ?? row.CreateCell(columnIndex);
cell.CellStyle = Common;
9,单元格部分文字颜色设置。
int rowIndex = ;
int columnIndex = ; var row = sheet.GetRow(rowIndex) ?? sheet.CreateRow(rowIndex);
var cell = row.GetCell(columnIndex) ?? row.CreateCell(columnIndex); IRichTextString richText;
if (sheet is HSSFSheet)
{
richText = new HSSFRichTextString("Microsoft OfficeTM");
}
else
{
richText = new XSSFRichTextString("Microsoft OfficeTM");
} //apply font to "Microsoft Office"
IFont redFont = workbook.CreateFont();
redFont.FontHeightInPoints = ;
redFont.Color = IndexedColors.Red.Index;
richText.ApplyFont(, , redFont); //apply font to "TM"
IFont blueFont = workbook.CreateFont();
blueFont.TypeOffset = FontSuperScript.Super;
blueFont.IsItalic = true;
blueFont.Color = IndexedColors.Blue.Index;
blueFont.FontHeightInPoints = ;
richText.ApplyFont(, , blueFont); var style = workbook.CreateCellStyle();
style.BorderBottom = BorderStyle.Thin;
style.BorderLeft = BorderStyle.Thin;
style.BorderRight = BorderStyle.Thin;
style.BorderTop = BorderStyle.Thin;
// 解决字体大小不一样问题
richText.ApplyFont(style.FontIndex);
cell.SetCellValue(richText);
cell.CellStyle = style;
实行结果:
这样通过NPOI基本的Excel操作就没问题了。
最新文章
- 借助node实战WebSocket
- [转]Android应用程序框架思路整理
- ASP.NET MVC中多种ActionResult用法总结
- iOS no visible @interface for &#39;UIButton&#39; declares the selector errors
- React Native使用AsyncStorage本地持久化
- XListView刷新
- Java-Eclipse插件开发学习笔记
- Phonegap hello world 不容易啊~!
- 函数flst_remove
- 1316. Electronic Auction(树状数组)
- 初识pngdrive
- C/C++基础知识总结——函数
- Java学习笔记之类和对象
- 格式化输出io:format的奇技淫巧
- Java获取图片属性
- 光流法-opencv python
- np.memmap读取大文件
- 【转载】Impala和Hive的区别
- getViewTreeObserver
- 【Mysql】大数据处理优化方法
热门文章
- 马凯军201771010116《面向对象与程序设计Java》第十六周知识学习总结
- 运维route语法
- (a ==1 &;&; a== 2 &;&; a==3) 有可能是 true 吗?
- Touch Gesture手势总结
- Linux环境下安装nginx
- 【图解】Hive文件存储格式
- redis的LRU算法(二)
- Apache HTTP 服务器 2.4(又名httpd)安装\配置 \启动
- flask中注册验证码和分页
- 请给Array本地对象增加一个原型方法,它用于删除数组条目中重复的条目(可能有多个),返回值是一个包含被删除的重复条目的新数组。