Aspose.Cells读取导入导出excel文件
using aspose.cells;
.......
public void importExcel(DataTable dt, string fileName, string filePath)
{
int posStart = 2;//报表内容起始
//设置字体样式
Style style1 = new Style();
style1.HorizontalAlignment = TextAlignmentType.Center;//文字居中
style1.VerticalAlignment = TextAlignmentType.Left;
style1.Font.Name = "宋体";
style1.Font.IsBold = true;//设置粗体
style1.Font.Size = 12;//设置字体大小
Style style2 = new Style();
style2.HorizontalAlignment = TextAlignmentType.Center;
style2.Font.Size = 10;
Workbook wb = new Workbook();
Worksheet ws = wb.Worksheets[0];
Cells cell = ws.Cells;
int colCount = dt.Columns.Count;
int rowCount = dt.Rows.Count;
//合并第一行单元格,显示标题
Range range = cell.CreateRange(0, 0, 1, colCount);
range.Merge();
cell["A1"].PutValue(fileName); //标题
cell.SetRowHeight(0, 20);//设置行高
//给单元格关联样式
cell["A1"].SetStyle(style1); //报表名字 样式
//设置Execl列名
for (int i = 0; i < colCount; i++)
{
cell[1, i].PutValue(dt.Columns[i].ColumnName);
//cell[1, i].SetStyle(style2);
}
//设置单元格内容
for (int i = 0; i < rowCount; i++)
{
for (int j = 0; j < colCount; j++)
{
cell[i + posStart, j].PutValue((dt.Rows[i][j] + "").Trim());
//cell[i + posStart, j].SetStyle(style2);
}
}
//设置列宽
//for (int i = 1; i < dt.Columns.Count; i++)
//{
// cell.SetColumnWidthPixel(i - 1, int.Parse(dataGridView1.Columns[i].Width.ToString()));
//}
//保存excel表格
wb.Save(filePath);
}