佛山品牌网站建设,wordpress 超过20M,如何在门户网站做搜索引擎,网站出现wordpressAspose.Cells是一款功能强大的Excel文档处理和转换控件#xff0c;开发人员和客户电脑无需安装Microsoft Excel也能在应用程序中实现类似Excel的强大数据管理功能。 1、获取工作薄Workbook
string excelFile C:\Users\Administrator\Desktop\FE.xlsx;
Workbook …Aspose.Cells是一款功能强大的Excel文档处理和转换控件开发人员和客户电脑无需安装Microsoft Excel也能在应用程序中实现类似Excel的强大数据管理功能。 1、获取工作薄Workbook
string excelFile C:\Users\Administrator\Desktop\FE.xlsx;
Workbook wb OpenWorkbook(excelFile);
2、获取工作表对象Worksheet
// 按序号
Worksheet sheet wb.Worksheets[0];
// 按sheet名
Worksheet sheet wb.Worksheets[sheet1];
3、获取Cells、Cell
// 获取Cells
Cells cells sheet.Cells
// 获取Cell序号从0开始
Cell cell cells[1,2]
4、读取、写入Cell值
// 读取cell值
var va cell.Value;
string va cell.StringValue;
...
// 写入cell值
cell.Value 这是一个写入值;
cell.PutValue(写入值);
5、获取并设置单元格样式
// 获取style
Style style cell.GetStyle();
// 数字型
style.Number 4;
// 数字格式
style.Custom 0.00;// 设置style
cell.SetStyle(style);
更多详细设置
style.HorizontalAlignment TextAlignmentType.Center;//文字居中
style.Font.Name 宋体;//文字字体
style.Font.Size 22;//文字大小
style.IsLocked false;//单元格解锁
style.Font.IsBold true;//粗体
style.ForegroundColor Color.FromArgb(0xaa, 0xcc, 0xbb);//设置背景色
style.Pattern BackgroundType.Solid; //设置背景样式
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; //应用边界线 下边界线
6、保存Workbook并释放
// 保存
wb.Save(excelFile);
wb.Dispose();
7、删除行
// 获取Cells
Cells cells sheet.Cells
// 删除单行序号从0开始
cells.DeleteRow(3);
// 删除多行参数2为要删的行数
cells.DeleteRows(3,10);
8、删除列
// 获取Cells
Cells cells sheet.Cells
// 删除列序号从0开始
cells.DeleteColumn(3);
// 删除多列参数2为要删的列数
cells.DeleteColumns(3,10);
9、复制行复制列同理
// 获取Cells
Cells cells sheet.Cells
// 复制单行从参数2复制到参数3
cells.CopyRow(cells, 83, 85);
// 复制多行参数4为要复制的行数
cells.CopyRows(cells, 83, 168 i * 85, 85);
10、强制更新计算公式、设置公式
//强制更新计算公式
wb.CalculateFormula();
//给单元格设置计算公式
cell.Formula AVERAGE(B1:E1);
11、设置行高列宽
//设置行高
cells.SetRowHeight(0, 20);
//设置列宽
cells.SetColumnWidth(1, 30);
12、合并、取消合并单元格
// 参数起始行起始列行数列数
cells.Merge(0, 0, 1, 5);
// 参数起始行起始列行数列数
cells.UnMerge(4, 2, 2, 3);
13、插入行、列
// 在序号处插入一行
cells.InsertRow(5);
// 参数2为插入的行数
cells.InsertRows(5,3);cells.InsertColumn(5);
cells.InsertColumns(5,3);
14、将字典dict的值映射写入Excel文件
public static void ExcelAttributeMapper(string excelPath, int sheet_in_col, int sheet_map_col, Dictionarystring, string dict, int startRow 0)
{// 打开工作薄Workbook wb OpenWorkbook(excelPath);// 打开工作表Worksheet sheet wb.Worksheets[0];// 逐行处理for (int i startRow; i sheet.Cells.MaxDataRow; i){// 获取目标cellCell inCell sheet.Cells[i, sheet_in_col];Cell mapCell sheet.Cells[i, sheet_map_col];// 属性映射if (inCell is not null dict.ContainsKey(inCell.StringValue)){mapCell.Value dict[inCell.StringValue]; // 赋值}}// 保存wb.Save(excelFile);wb.Dispose();
}
15、简单的写入值
public static void ExcelWriteCell(string excelPath, int row, int col, string cell_value)
{// 打开工作薄Workbook wb OpenWorkbook(excelPath);// 打开工作表Worksheet sheet wb.Worksheets[0];// 获取cellCell cell sheet.Cells[row, col];// 写入cell值cell.Value cell_value;// 保存wb.Save(excelFile);wb.Dispose();
}
16、Excel设置单元格的格式
public static void ExcelSetColStyle(string excelPath, int col, int startRow, int styleNumber 4, int digit 2)
{// 打开工作薄Workbook wb OpenWorkbook(excelPath);// 打开工作表Worksheet sheet wb.Worksheets[0];for (int i startRow; i sheet.Cells.MaxDataRow; i){// 获取cellCell cell sheet.Cells[i, col];// 获取styleStyle style cell.GetStyle();// 数字型style.Number styleNumber;// 小数位数if (digit 1) { style.Custom 0.0; }else if (digit 2) { style.Custom 0.00; }else if (digit 3) { style.Custom 0.000; }else if (digit 4) { style.Custom 0.0000; }}// 保存wb.Save(excelFile);wb.Dispose();
}
17、删除Excel表中的0值行【指定多个列】
private static Listint ExcelDeleteNullRowResult(string excelPath, Listint deleteCols, int startRow 0)
{Listint list new Listint();// 打开工作薄Workbook wb OpenWorkbook(excelPath);// 打开工作表Worksheet sheet wb.Worksheets[0];// 强制更新表内的公式单元格wb.CalculateFormula();// 找出0值行for (int i sheet.Cells.MaxDataRow; i startRow; i--){// 设置一个flagbool isNull true;// 循环查找各列的值foreach (var deleteCol in deleteCols){Cell cell sheet.Cells.GetCell(i, deleteCol);if (cell ! null) // 值不为空{string str cell.StringValue;if (str ! ) // 值不为0{if (double.Parse(str) ! 0){isNull false;break;}}}}// 输出删除列if (isNull){list.Add(i);}}// 保存wb.Save(excelFile);wb.Dispose();// 返回值return list;
}
18、从Excel文件中获取Dictionary
public static Dictionarystring, string GetDictFromExcel(string excelPath, int col1 0, int col2 1)
{// 定义字典Dictionarystring, string dict new Dictionarystring, string();// 打开工作薄Workbook wb OpenWorkbook(excelPath);// 打开工作表Worksheet sheet wb.Worksheets[0];// 获取key和value值for (int i 0; i sheet.Cells.MaxDataRow; i){Cell key sheet.Cells[i, col1];Cell value sheet.Cells[i, col2];if (key ! null value ! null){if (!dict.ContainsKey(key.StringValue)){if (key.StringValue ! value.StringValue ! ) // 空值不纳入{dict.Add(key.StringValue, value.StringValue);}}}}wb.Dispose();// 返回dictreturn dict;
}