using Aspose.Cells;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Runtime.Serialization;
|
|
using System.Runtime.Serialization.Formatters.Binary;
|
|
using System.Text;
|
|
using System.Web;
|
|
|
|
|
|
namespace Utils.Datas
|
|
{
|
|
|
|
/// <summary>
|
|
/// Excel操作帮助类 (引用程序集Aspose.Cells.dll)
|
|
/// </summary>
|
|
public class ExcelHelper
|
|
{
|
|
/// <summary>
|
|
/// 读取指定路径的Excel文件为DataTable
|
|
/// </summary>
|
|
/// <param name="fileName">文件全路径</param>
|
|
/// <param name="firstRow">要开始读取的第一行在Excel中的行索引(默认会以此行各列数据作为DataTable的列名)</param>
|
|
/// <param name="firstColumn">要开始读取的第一列在Excel中的列索引</param>
|
|
/// <param name="invalidEndRowNumber">文件尾部无效行数目(该部分所有行数据将不会被读取)</param>
|
|
/// <returns>返回DataSet</returns>
|
|
public static DataSet ReadExcel(string fileName, int firstRow = 0, int firstColumn = 0, int invalidEndRowNumber = 0)
|
|
{
|
|
DataSet ds = new DataSet();
|
|
try {
|
|
if (string.IsNullOrEmpty(fileName) || !System.IO.File.Exists(fileName)) {
|
|
return null;
|
|
}
|
|
//new Aspose.Cells.License().SetLicense(AsposeHelper.LStream);
|
|
Cells cells;
|
|
Workbook workbook = new Workbook(fileName);
|
|
foreach (Worksheet worksheet in workbook.Worksheets) {
|
|
cells = worksheet.Cells;//取Sheet读取
|
|
if (cells == null) {
|
|
return null;
|
|
}
|
|
try {
|
|
DataTable dt = cells.ExportDataTable(firstRow, firstColumn, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, true);
|
|
dt.TableName = worksheet.Name;//取Sheet的名字作为表名
|
|
if (dt != null && dt.Rows.Count > 0) {
|
|
ds.Tables.Add(dt);
|
|
}
|
|
} catch (Exception e) {
|
|
LogUtil.LogError("读取Excel内容失败:worksheet.Name" + worksheet.Name + " " + e.Message);
|
|
}
|
|
}
|
|
|
|
} catch (Exception ex) {
|
|
LogUtil.LogError("读取Excel内容失败:" + ex.Message);
|
|
}
|
|
return ds;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 将DataTable导出指定路径的Excel文件
|
|
/// </summary>
|
|
/// <param name="dt">DataTable</param>
|
|
/// <param name="tableName">Excel文件的表头内容</param>
|
|
/// <param name="path">文件保存的全路径</param>
|
|
public static void ExportExcel(DataTable dt, string headerText, string path)
|
|
{
|
|
Workbook workbook = new Workbook(); //工作簿
|
|
Worksheet sheet = workbook.Worksheets[0]; //工作表
|
|
Cells cells = sheet.Cells;//单元格
|
|
|
|
//为标题设置样式
|
|
Style styleTitle = workbook.Styles[workbook.Styles.Add()];//新增样式
|
|
styleTitle.HorizontalAlignment = TextAlignmentType.Center;//文字居中
|
|
styleTitle.Font.Name = "宋体";//文字字体
|
|
styleTitle.Font.Size = 18;//文字大小
|
|
styleTitle.Font.IsBold = true;//粗体
|
|
|
|
//样式2
|
|
Style style2 = workbook.Styles[workbook.Styles.Add()];//新增样式
|
|
style2.HorizontalAlignment = TextAlignmentType.Center;//文字居中
|
|
style2.Font.Name = "宋体";//文字字体
|
|
style2.Font.Size = 14;//文字大小
|
|
style2.Font.IsBold = true;//粗体
|
|
style2.IsTextWrapped = true;//单元格内容自动换行
|
|
style2.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
|
|
style2.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
|
|
style2.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
|
|
style2.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
|
|
|
|
//样式3
|
|
Style style3 = workbook.Styles[workbook.Styles.Add()];//新增样式
|
|
style3.HorizontalAlignment = TextAlignmentType.Center;//文字居中
|
|
style3.Font.Name = "宋体";//文字字体
|
|
style3.Font.Size = 12;//文字大小
|
|
style3.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
|
|
style3.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
|
|
style3.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
|
|
style3.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
|
|
|
|
int Colnum = dt.Columns.Count;//表格列数
|
|
int Rownum = dt.Rows.Count;//表格行数
|
|
|
|
//生成行1 标题行
|
|
cells.Merge(0, 0, 1, Colnum);//合并单元格
|
|
cells[0, 0].PutValue(headerText);//填写内容
|
|
cells[0, 0].SetStyle(styleTitle);
|
|
cells.SetRowHeight(0, 38);
|
|
|
|
//生成行2 列名行
|
|
for (int i = 0; i < Colnum; i++) {
|
|
cells[1, i].PutValue(dt.Columns[i].ColumnName);
|
|
cells[1, i].SetStyle(style2);
|
|
cells.SetRowHeight(1, 25);
|
|
}
|
|
|
|
//生成数据行
|
|
for (int i = 0; i < Rownum; i++) {
|
|
for (int k = 0; k < Colnum; k++) {
|
|
cells[2 + i, k].PutValue(dt.Rows[i][k].ToString());
|
|
cells[2 + i, k].SetStyle(style3);
|
|
}
|
|
cells.SetRowHeight(2 + i, 24);
|
|
}
|
|
|
|
workbook.Save(path);
|
|
}
|
|
}
|
|
}
|