using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Data;
using System.IO;
using System.Data.OleDb;
using Aspose.Cells;
using System.Reflection; namespace Utils
{
/// <summary>
/// 解析Excel文件
/// </summary>
public class ExcelUitl
{
public static DataSet ExcelToDS(string Path)
{
DataSet ds = new DataSet(); Workbook workbook = new Workbook();
workbook.Open(Path);
Cells cells = workbook.Worksheets[0].Cells;
DataTable dt = cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, true);
ds.Tables.Add(dt);
return ds;
}
public static DataSet ExcelToDS(string Path, string sheetName)
{
DataSet ds = new DataSet(); Workbook workbook = new Workbook();
workbook.Open(Path); Worksheet sheet = workbook.Worksheets[sheetName];
if (sheet == null)
{
return null;
}
Cells cells = sheet.Cells;
DataTable dt = cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, true);
ds.Tables.Add(dt);
return ds;
}
public static DataSet GetTableList(string Path)
{
DataSet ds = new DataSet(); Workbook workbook = new Workbook();
workbook.Open(Path);
if (workbook.Worksheets != null)
{
foreach (Worksheet sheet in workbook.Worksheets)
{
Cells cells = sheet.Cells; DataTable dt = cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, true);
dt.TableName = sheet.Name;
ds.Tables.Add(dt);
}
}
return ds;
} /// <summary>
/// </summary>
/// <param name="tablelist"></param>
/// <param name="path"></param>
/// <param name="error"></param>
public static void ImportAndZip(List<DataTable> tablelist, string excelFilePath)
{
Workbook workbook = new Workbook();
int sheetIndex = 0; string dir = excelFilePath.Replace(Path.GetFileName(excelFilePath), "");
string txtdir = Path.Combine(dir, "sco");
if (!Directory.Exists(txtdir))
{
Directory.CreateDirectory(txtdir);
} DataTable error = new DataTable();
error.TableName = "导出情况表";
error.Columns.Add(new DataColumn()
{
ColumnName = "错误工作簿名称",
});
error.Columns.Add(new DataColumn()
{
ColumnName = "错误工作簿行",
});
error.Columns.Add(new DataColumn()
{
ColumnName = "错误工作簿列名",
});
error.Columns.Add(new DataColumn()
{
ColumnName = "错误原因",
});
error.Columns.Add(new DataColumn()
{
ColumnName = "导入路径",
}); foreach (DataTable table in tablelist)
{
Worksheet sheet = null;
if (sheetIndex == 0)
{
sheet = workbook.Worksheets[sheetIndex];
sheet.Name = table.TableName;
}
else
{
sheet = workbook.Worksheets.Add(table.TableName);
} //标题
int indexX = 0, indexY = 0;
foreach (DataColumn column in table.Columns)
{
sheet.Cells[indexX, indexY].PutValue(column.ColumnName);
indexY++;
} int Colnum = table.Columns.Count;//表格列数
int Rownum = table.Rows.Count;//表格行数 //生成数据行
for (int i = 0; i < Rownum; i++)
{
for (int k = 0; k < Colnum; k++)
{
try
{
//单元格最大存 32k内容
sheet.Cells[1 + i, k].PutValue(table.Rows[i][k].ToString()); //添加数据
}
catch (Exception ex)
{
string message = "";
string txtName = table.TableName + "_" + i + ".txt";
string txtPath = "";
if (ex.Message.ToString().Contains("MS Excel only allows to put a string shorter than 32K to a Cell"))
{
message = "单元格数据大于32K不能写入excel";
//将内容写入txt
File.WriteAllText(Path.Combine(txtdir, txtName), table.Rows[i][k].ToString());
txtPath = "sco/" + txtName;
}
else
{
message = ex.Message.ToString();
} //sun: 当数据太大。我们不导入excel,将文本导入txt,路径路径
error.Rows.Add(new object[] { sheet.Name, i, table.Columns[k].ColumnName, message, txtPath });
}
}
}
SetHeaderStyle(sheet); //sheet个数
sheetIndex++;
} //将错误信息表也写入excel
Worksheet errrsheet = workbook.Worksheets.Add(error.TableName);
for (int i = 0; i < error.Columns.Count; i++)
{
errrsheet.Cells[0, i].PutValue(error.Columns[i].ColumnName);
}
//foreach (DataColumn column in error.Columns)
//{
// errrsheet.Cells[0, 0].PutValue(column.ColumnName);
//}
int eColnum = error.Columns.Count;//表格列数
int eRownum = error.Rows.Count;//表格行数
for (int i = 0; i < eRownum; i++)
{
for (int k = 0; k < eColnum; k++)
{
errrsheet.Cells[1 + i, k].PutValue(error.Rows[i][k].ToString()); //添加数据
}
}
SetHeaderStyle(errrsheet); workbook.Save(excelFilePath);
} /// <summary>
/// 创建excel
/// </summary>
/// <param name="tablelist"></param>
/// <param name="path"></param>
public static void CreateExcel(List<DataTable> tablelist, string path)
{
Workbook workbook = new Workbook();
int sheetIndex = 0;
foreach (DataTable table in tablelist)
{
Worksheet sheet = null;
if (sheetIndex == 0)
{
sheet = workbook.Worksheets[sheetIndex];
sheet.Name = table.TableName;
}
else
{
sheet = workbook.Worksheets.Add(table.TableName);
} //标题
int indexX = 0, indexY = 0;
foreach (DataColumn column in table.Columns)
{
sheet.Cells[indexX, indexY].PutValue(column.ColumnName);
indexY++;
} int Colnum = table.Columns.Count;//表格列数
int Rownum = table.Rows.Count;//表格行数 //生成数据行
for (int i = 0; i < Rownum; i++)
{
for (int k = 0; k < Colnum; k++)
{
try
{
//单元格最大存 32k内容
sheet.Cells[1 + i, k].PutValue(table.Rows[i][k].ToString()); //添加数据 }
catch (Exception ex)
{
//sun: 当数据太大。我们不导入 }
}
}
SetHeaderStyle(sheet); //sheet个数
sheetIndex++;
} workbook.Save(path);
}
public static void SetHeaderStyle(Worksheet sheet)
{
#region header style
Style style = new Style();
style.Font.IsBold = true;
style.Font.Size = 12;
StyleFlag styleFlag = new StyleFlag
{
FontBold = true,
FontSize = true,
};
sheet.Cells.ApplyRowStyle(0, style, styleFlag);
sheet.AutoFitColumns(); #endregion
} public static void SetBodyStyle(Worksheet sheet, int column)
{
#region header style
Style style = new Style();
style.Font.IsBold = false;
style.Font.Size = 11;
StyleFlag styleFlag = new StyleFlag
{
FontBold = true,
FontSize = true
};
sheet.Cells.ApplyColumnStyle(column, style, styleFlag);
sheet.AutoFitColumns(); #endregion
}
} }

  

常用类-Excel-使用Aspose.Cells插件的更多相关文章

  1. csharp: read excel using Aspose.Cells

    /// <summary> /// /// </summary> /// <param name="strFileName"></para ...

  2. 常用类-excel转csv

    public class ExcelFileHelper { public static bool SaveAsCsv(string excelFilePath, string destination ...

  3. 使用Aspose.Cells读取Excel

      最新更新请访问: http://denghejun.github.io Aspose.Cells读取Excel非常方便,以下是一个简单的实现读取和导出Excel的操作类: 以下是Aspose.Ce ...

  4. ASPOSE.Cells & ASPOSE.Words 操纵Excel和Word文档的 .NET Core 实例

    Aspose.Total是Aspose公司旗下的最全的一套office文档管理方案,它提供的原生API可以对Word.Excel.PDF.Powerpoint.Outlook.CAD.图片.3D.ZI ...

  5. Aspose.Cells基础使用方法整理

    Aspose.Cells 插件,将web端数据以excel形式导出到客户端. 相关文档: https://blog.csdn.net/djk8888/article/details/53065416 ...

  6. 基于 Aspose.Cells与XML导入excel 数据----操作类封装

    前言 导入excel数据, 在每个项目中基本上都会遇到,第三方插件或者基于微软office,用的最多的就是npoi,aspose.cells和c#基于office这三种方式,其中各有各的优缺点,在这也 ...

  7. EpPlus读取生成Excel帮助类+读取csv帮助类+Aspose.Cells生成Excel帮助类

    大部分功能逻辑都在,少量自定义异常类和扩展方法 ,可用类似代码自己替换 //EpPlus读取生成Excel帮助类+读取csv帮助类,epplus只支持开放的Excel文件格式:xlsx,不支持 xls ...

  8. NPOI、MyXls、Aspose.Cells 导入导出Excel(转)

    Excel导入及导出问题产生: 从接触.net到现在一直在维护一个DataTable导s出到Excel的类,时不时还会维护一个导入类.以下是时不时就会出现的问题: 导出问题: 如果是asp.net,你 ...

  9. C# WinForm 导出导入Excel/Doc 完整实例教程[使用Aspose.Cells.dll]

    [csharp] view plain copy 1.添加引用: Aspose.Cells.dll(我们就叫工具包吧,可以从网上下载.关于它的操作我在“Aspose.Cells操作说明 中文版 下载 ...

随机推荐

  1. MVC模式与Servlet执行流程

    ##Servlet生命周期 五个部分,从加载到卸载,如同人类的出生到死亡 加载:Servlet容器自动处理 初始化:init方法 该方法会在Servlet被加载并实例化后执行 服务:service抽象 ...

  2. .Net C# 时间戳时间转换

    闲话不多说,直接上代码 /// <summary> /// 时间拓展 /// </summary> public static class DateTimeExtension ...

  3. CouchDB学习一

    端口 端口号 协议 作用 5984 tcp 标椎集群端口用于所有的HTTP API请求 5986 tcp 用于管理员对节点与分片的管理 4369 tcp Erlang端口到daemon的映射 配置介绍 ...

  4. requests第三方库

    requests第三方库 简介: requests是一个优雅而简单的Python 第三方HTTP请求库,专为人类而构建. requests的官方文档同样也非常的完善详尽,而且少见的有中文官方文档:ht ...

  5. php有必要用swoole吗

    在 Swoole 官网的自我介绍是“面向生产环境的 PHP 异步网络通信引擎”,首先 Swoole 它是一个网络应用的开发工具,它支持 Http.TCP.UDP.WebSocket. Swoole 和 ...

  6. Go 开发关键技术指南 | 为什么你要选择 GO?(内含超全知识大图)

    作者 | 杨成立(忘篱) 阿里巴巴高级技术专家 关注"阿里巴巴云原生"公众号,回复 Go 即可查看清晰知识大图! 导读:从问题本身出发,不局限于 Go 语言,探讨服务器中常常遇到的 ...

  7. 【HNOI 2019】JOJO

    Problem Description JOJO 的奇幻冒险是一部非常火的漫画.漫画中的男主角经常喜欢连续喊很多的「欧拉」或者「木大」. 为了防止字太多挡住漫画内容,现在打算在新的漫画中用 \(x\) ...

  8. JavaScript Map 和 Set

    结论 Map:存放键值对,区别于 Object,键可以是任何值. Set:存放不重复的值 Map 存储键值对,读取时与插入顺序一致. var map = new Map([[1, "1&qu ...

  9. [转]BEC Vantage

    https://www.examenglish.com/BEC/BEC_Vantage.html https://www.cambridgeenglish.org/exams-and-tests/bu ...

  10. Linux中jar包启动和jar包后台运行

    Linux 运行jar包命令如下: 方式一: java -jar shareniu.jar 特点:当前ssh窗口被锁定,可按CTRL + C打断程序运行,或直接关闭窗口,程序退出 那如何让窗口不锁定? ...