【C#】Excel操作汇总


一、Excel多种操作方法

1、DataSet到Excel的文件读写

2、DataTable与Excel的文件读写

3、NPOI使用方法

(1)保存与读取

https://my.oschina.net/u/4399215/blog/3435686
https://blog.csdn.net/jkhmf/article/details/82946965

(2)利用NPOI在同一个Excel文件中创建多个sheet


(3)设置EXCEL单元格数字格式

https://blog.csdn.net/xxs77ch/article/details/50237017

https://bbs.csdn.net/topics/390938286

(4)设置EXCEL单元格背景填充色

https://bbs.csdn.net/topics/390203526

4、Aspose.Cells.dll使用方法

(1)导出导入Excel/Doc


(2)设置单元格样式

//Instantiating a Workbook object
Workbook workbook = new Workbook();
//Adding a new worksheet to the Workbook object
int i = workbook.Worksheets.Add();
//Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
//Adding the current system date to "A1" cell
worksheet.Cells["A1"].PutValue(DateTime.Now);
//Getting the Style of the A1 Cell
Style style = worksheet.Cells["A1"].GetStyle();
//Setting the display format to number 15 to show date as "d-mmm-yy"
style.Number = 15;
//Applying the style to the A1 cell
worksheet.Cells["A1"].SetStyle(style);
//Adding a numeric value to "A2" cell
worksheet.Cells["A2"].PutValue(20);
//Getting the Style of the A2 Cell
style = worksheet.Cells["A2"].GetStyle();
//Setting the display format to number 9 to show value as percentage
style.Number = 9;
//Applying the style to the A2 cell
worksheet.Cells["A2"].SetStyle(style);
//Adding a numeric value to "A3" cell
worksheet.Cells["A3"].PutValue(2546);
//Getting the Style of the A3 Cell
style = worksheet.Cells["A3"].GetStyle();
//Setting the display format to number 6 to show value as currency
style.Number = 6;
//Applying the style to the A3 cell
worksheet.Cells["A3"].SetStyle(style);
//Saving the Excel file
workbook.Save("C:\\book1.xls", SaveFormat.Excel97To2003);
当然开发人员还可以为单元格设置自定义显示样式,下面的代码就怎么设置单元格自定义显示样式做举例:
//Instantiating a Workbook object
Workbook workbook = new Workbook();
//Adding a new worksheet to the Excel object
int i = workbook.Worksheets.Add();
//Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
//Adding the current system date to "A1" cell
worksheet.Cells["A1"].PutValue(DateTime.Now);
//Getting the style of A1 cell
Style style = worksheet.Cells["A1"].GetStyle();
//Setting the custom display format to show date as "d-mmm-yy"
style.Custom = "d-mmm-yy";
//Applying the style to A1 cell
worksheet.Cells["A1"].SetStyle(style);
//Adding a numeric value to "A2" cell
worksheet.Cells["A2"].PutValue(20);
//Getting the style of A2 cell
style = worksheet.Cells["A2"].GetStyle();
//Setting the custom display format to show value as percentage
style.Custom = "0.0%";
//Applying the style to A2 cell
worksheet.Cells["A2"].SetStyle(style);
//Adding a numeric value to "A3" cell
worksheet.Cells["A3"].PutValue(2546);
//Getting the style of A3 cell
style = worksheet.Cells["A3"].GetStyle();
//Setting the custom display format to show value as currency
style.Custom = "£#,##0;[Red]$-#,##0";
//Applying the style to A3 cell
worksheet.Cells["A3"].SetStyle(style);
//Saving the Excel file
workbook.Save("C:\\book1.xls", SaveFormat.Excel97To2003);
using Aspose.Cells;    
using System.Drawing;
 
Workbook workbook=new Workbook();
Style style=workbook.Styles[workbook.Styles.Add()];
WorkSheet worksheet=workbook.WorkSheet[0];
//字体样式
style.Font.Color = Color.Red;//字体颜色
style.Font.Size = 10;//字体大小
style.Font.IsBold = true;//字体加粗
style.Font.Name = "宋体";//文字字体 
 
//单元格样式
//单元格背景颜色
style.ForegroundColor = Color.Red;//红色
style.ForegroundColor = Color.Gray;//灰色
style.ForegroundColor = Color.Yellow;//黄色
style.ForegroundColor = Color.Magenta;//紫红色
style.ForegroundColor = Color.Orange;//橙色
style.ForegroundColor = Color.Pink;//粉红
style.ForegroundColor = Color.Aqua;//浅蓝
style.ForegroundColor = Color.PaleGreen;//浅绿
 
style.Pattern = BackgroundType.Solid;
style.HorizontalAlignment = TextAlignmentType.Center;//水平居中
style.IsTextWrapped = true;//单元格内容自动换行
//边框样式
style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin; //左边框 
style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; //右边框  
style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin; //上边框  
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin; //下边框
 
Range range= worksheet.Cells.CreateRange(0, 0, 1, 1);//第一行第一列单元格
range.ApplyStyle(style, new StyleFlag() { All=true});

https://blog.csdn.net/qq_38974638/article/details/108631101

5、spire.xls使用方法

(1)应用实例datagridview→.xlsx

https://blog.csdn.net/binbingbong/article/details/106051194

(2)给Excel添加、删除数字签名

6、生成pdf

(1)iTextSharp方法

向pdf插入图片:https://blog.csdn.net/weixin_41529093/article/details/104978740

生成pdf文件流:

(2)Spire.Pdf方法

使用汇总:

给PDF添加可见的数字签名:

二、大数据导出到Excel

(1)大量数据导出到Excel



(2).NET下,实现大txt文件读取并写入excel大文件

https://blog.csdn.net/golduty2/article/details/89473030

三、读取Excel单元格字符串截断问题

1、问题描述:
表格某一单元格内容过长,则上位机读取Excel表时,字符串将被截断,导致数据缺失,对后续数据处理造成错误。

2、问题分析:
目前微软提供了两种Office Ole驱动来实现数据库连接,分别为ACE版本和Jet版本。由于Jet引擎只可以访问 Office 97-2003,故上位机代码中采用了调用Microsoft.ACE.OLEDB.12.0驱动的方式,利用Microsoft ACE引擎连接到工作簿,将Excel文件作为数据源来读写。

而OleDb读取文件的方式受Excel ISAM(Indexed Sequential Access Method,即索引顺序存取方法)驱动程序的限制。在读取过程中,Excel ISAM 驱动程序通过扫描sheet中前几行(默认为8行)的内容来确定一个 Excel列的最合适的数据类型,然后选择能够代表其样本中大部分值的数据类型,分配存储大小。该数据类型通常为varchar,其大小为255字符。而实际使用中,常常会出现该列前8行字符串在255个字符以内,8行以外的字符串却大于255个字符的情况,那么这个时候使用OleDb读取Excel就会将后面大于255的字符串截断。

3、改善措施与验证结果:
经翻查资料,得到的解决方案为,通过修改注册表值来更改采样行数。修改路径如下(Excel 2016):
计算机\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\16.0\Access Connectivity Engines\Excel
TypeGuessRows取值范围为0-16。当TypeGuessRows = 0时会扫描整个页面再匹配合适的数据类型。但试验后发现,该方法只适用于Jet引擎,对于使用ACE引擎的上位机,该方法无效。
在不修改表格读取方式的情况下,一个可以实现字符串正常读取的措施是,把数据大于255的表格放到前8行。在前8行的判定中,数据已经超过255,则该列的数据类型会被设定得更大,就不会出现截断的情况。
由于该方法会增加额外的工作量,不宜作为永久措施。目前的最佳方案是修改文件读取方式,用NPOI方法读取Excel表格。该方法不受ISAM驱动程序限制,可以保证字符串的完整性,同时可以满足读取高版本Excel文件的需求。
经调试,可以确保该方法能够完整读取单元格长度远大于255的字符串。

4、总结:
在处理单一、长度较短的表格时,可以优先选择OleDb方式对表格进行读取;若每一单元格包含的数据类型较为复杂,则推荐在编写上位机时使用NPOI方法读取表格,保证数据处理的可靠性。

相关