using Aspose.Cells;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace AsposeDLL
{
public class Aspose_Excel
{
/// <summary>
/// 数据表导出Excel
/// </summary>
/// <param name="data">数据表</param>
/// <param name="filepath">导出路径</param>
/// <returns></returns>
public static bool ExportExcelWithAspose(System.Data.DataTable data, string filepath)
{
try
{
if (data == null)
{
MessageBox.Show("数据为空");
return false;
}
//Aspose.Cells.License li = new Aspose.Cells.License();
//li.SetLicense("ASPOSE/License.lic");//破解证书
Workbook book = new Workbook(); //创建工作簿
Worksheet sheet = book.Worksheets[0]; //创建工作表
Cells cells = sheet.Cells; //单元格
//创建样式
global::Aspose.Cells.Style style = book.Styles[book.Styles.Add()];
style.Borders[global::Aspose.Cells.BorderType.LeftBorder].LineStyle = global::Aspose.Cells.CellBorderType.Thin; //应用边界线 左边界线
style.Borders[global::Aspose.Cells.BorderType.RightBorder].LineStyle = global::Aspose.Cells.CellBorderType.Thin; //应用边界线 右边界线
style.Borders[global::Aspose.Cells.BorderType.TopBorder].LineStyle = global::Aspose.Cells.CellBorderType.Thin; //应用边界线 上边界线
style.Borders[global::Aspose.Cells.BorderType.BottomBorder].LineStyle = global::Aspose.Cells.CellBorderType.Thin; //应用边界线 下边界线
style.HorizontalAlignment = TextAlignmentType.Center; //单元格内容的水平对齐方式文字居中
style.Font.Name = "宋体"; //字体
//style1.Font.IsBold = true; //设置粗体
style.Font.Size = 11; //设置字体大小
//style.ForegroundColor = System.Drawing.Color.FromArgb(153, 204, 0); //背景色
//style.Pattern = Aspose.Cells.BackgroundType.Solid;
int Colnum = data.Columns.Count;//表格列数
int Rownum = data.Rows.Count;//表格行数
//生成行 列名行
for (int i = 0; i < Colnum; i++)
{
cells[0, i].PutValue(data.Columns[i].ColumnName); //添加表头
cells[0, i].SetStyle(style); //添加样式
}
//生成数据行
for (int i = 0; i < Rownum; i++)
{
for (int k = 0; k < Colnum; k++)
{
cells[1 + i, k].PutValue(data.Rows[i][k].ToString()); //添加数据
cells[1 + i, k].SetStyle(style); //添加样式
}
}
sheet.AutoFitColumns(); //自适应宽
book.Save(filepath); //保存
//book.Save(filepath,SaveFormat.Pdf); //保存
//MessageBox.Show("Excel成功保存到D盘!!!");
GC.Collect();
}
catch (Exception e)
{
return false;
}
return true;
}
/// <summary>
/// Excel导入
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public static DataTable ImportExcel(string filePath)
{
DataTable dt = new DataTable();
try
{
//打开文件,参数可以是文件的路径,也可以直接传入一个文件流
Workbook workbook = new Workbook(filePath);
//获取sheet表
WorksheetCollection worksheets = workbook.Worksheets;
Worksheet worksheet = null;
Cells cell = null;
//默认第一行为列名 所以第一行不读,索引从第二行开始
int rowIndex = 0;
//从第一列读取
int colIndex = 0;
for (int i = 0; i < worksheets.Count; i++)
{
worksheet = worksheets[i];
//获取每个sheet表的所有单元格
cell = worksheet.Cells;
dt = cell.ExportDataTableAsString(rowIndex, colIndex, cell.MaxDataRow + 1, cell.MaxDataColumn + 1, true);
//表
dt.TableName = "table" + i.ToString();
break;
}
worksheets.Clear();
worksheet = null;
worksheets = null;
workbook = null;
}
catch (Exception ex)
{
throw;
}
return dt;
}
}
}
基于Aspose Excel导出和导入
最新推荐文章于 2025-04-08 14:17:58 发布