基于Aspose Excel导出和导入

本文介绍了一种使用Aspose.Cells库在C#中进行Excel数据导入和导出的方法。文章详细展示了如何将DataTable数据导出到Excel,并从Excel文件中读取数据到DataTable。通过代码示例,读者可以了解到如何设置单元格样式、自适应列宽以及处理异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值