C# WinForm使用Aspose.Cells.dll 导出导入Excel/Doc 完整实例教程
1.添加引用:
Aspose.Cells.dll(我们就叫工具包吧,可以从网上下载。关于它的操作我在“Aspose.Cells操作说明 中文版 下载 Aspose C# 导出Excel 实例”一文中的说。这里你暂时也可不理会它。)
即使没有安装office也能用噢,这是一个好强的大工具。
2.编写Excel操作类
using System;
using System.Collections.Generic;
using System.Text;
using Aspose.Cells;
using System.Data;
public class AsposeExcel
{
private string outFileName = "";
private string fullFilename = "";
private Workbook book = null;
private Worksheet sheet = null;
public AsposeExcel(string outfilename, string tempfilename) //导出构造数
{
outFileName = outfilename;
book = new Workbook();
// book.Open(tempfilename);这里我们暂时不用模板
sheet = book.Worksheets[0];
}
public AsposeExcel(string fullfilename) //导入构造数
{
fullFilename = fullfilename;
// book = new Workbook();
// book.Open(tempfilename);
// sheet = book.Worksheets[0];
}
private void AddTitle(string title, int columnCount)
{
sheet.Cells.Merge(0, 0, 1, columnCount);
sheet.Cells.Merge(1, 0, 1, columnCount);
Cell cell1 = sheet.Cells[0, 0];
cell1.PutValue(title);
cell1.Style.HorizontalAlignment = TextAlignmentType.Center;
cell1.Style.Font.Name = "黑体";
cell1.Style.Font.Size = 14;
cell1.Style.Font.IsBold = true;
Cell cell2 = sheet.Cells[1, 0];
cell1.PutValue("查询时间:" + DateTime.Now.ToLocalTime());
cell2.SetStyle(cell1.Style);
}
private void AddHeader(DataTable dt)
{
Cell cell = null;
for (int col = 0; col < dt.Columns.Count; col++)
{
cell = sheet.Cells[0, col];
cell.PutValue(dt.Columns[col].ColumnName);
cell.Style.Font.IsBold = true;
}
}
private void AddBody(DataTable dt)
{
for (int r = 0; r < dt.Rows.Count; r++)
{
for (int c = 0; c < dt.Columns.Count; c++)
{
sheet.Cells[r + 1, c].PutValue(dt.Rows[R][c].ToString());
}
}
}
//导出------------下一篇会用到这个方法
public Boolean DatatableToExcel(DataTable dt)
{
Boolean yn = false;
try
{
//sheet.Name = sheetName;
//AddTitle(title, dt.Columns.Count);
//AddHeader(dt);
AddBody(dt);
sheet.AutoFitColumns();
//sheet.AutoFitRows();
book.Save(outFileName);
yn = true;
return yn;
}
catch (Exception e)
{
return yn;
// throw e;
}
}
public DataTable ExcelToDatatalbe()//导入
{
Workbook book = new Workbook();
book.Open(fullFilename);
Worksheet sheet = book.Worksheets[0];
Cells cells = sheet.Cells;
//获取excel中的数据保存到一个datatable中
DataTable dt_Import = cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, false);
// dt_Import.
return dt_Import;
}
}[/R]
3. Word导出
//设置文件类型
// saveFileDialog为一个对话框控件
//如果没有人工具栏中拉,
//可以:SaveFileDialog saveFileDialog1=new SaveFileDialog();
saveFileDialog1.Filter = "导出Excel (*.xls)|*.xls|Word (*.doc)|*.doc";
saveFileDialog1.FilterIndex = 1;
saveFileDialog1.RestoreDirectory = true;
saveFileDialog1.CreatePrompt = true;
saveFileDialog1.Title = "导出文件保存路径";
//saveFileDialog1.ShowDialog();
//string strName = saveFileDialog1.FileName;
//设置默认文件类型显示顺序
//saveFileDialog1.FilterIndex = 2;
//保存对话框是否记忆上次打开的目录
saveFileDialog1.RestoreDirectory = true;
//点了保存按钮进入
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
//获得文件路径
string localFilePath = saveFileDialog1.FileName.ToString();
//获取文件名,不带路径
string fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("\") + 1);
//获取文件路径,不带文件名
string FilePath = localFilePath.Substring(0, localFilePath.LastIndexOf("\"));
//给文件名前加上时间
string newFileName = DateTime.Now.ToString("yyyyMMdd") + fileNameExt;
//在文件名里加字符
//saveFileDialog1.FileName.Insert(1,"dameng");
saveFileDialog1.FileName = FilePath + "\" + newFileName;
System.IO.FileStream fs = (System.IO.FileStream)saveFileDialog1.OpenFile();//输出文件
StreamWriter writer = new StreamWriter(fs);
writer.Write("tttt");//这里就是你要导出到word的内容,内容是你什么你自已DIY
writer.Flush();
writer.Close();
fs.Close();
}
4. 导出datatable到excel
DataTable dt = null;
if (ds_all.Tables[0] != null)
{
dt = ds_all.Tables[0];
}
else {
MessageBox.Show("没有数据记录", "*^_^* 温馨提示信息", MessageBoxButtons.OK);
return;
}
//上面只是取datatable,你自己diy
AsposeExcel tt = new AsposeExcel(saveFileDialog1.FileName, "");//不用模板, saveFileDialog1是什么?上面已经说过
bool OK_NO = tt.DatatableToExcel(dt);
if (OK_NO)
{
MessageBox.Show("导出成功", "*^_^* 温馨提示信息", MessageBoxButtons.OK);
}
else
{
}
5. Excel导入
private void 导入ToolStripMenuItem_Click(object sender, EventArgs e)
{
string localFilePath = "";
//点了保存按钮进入
if (openFileDialog1.ShowDialog() == DialogResult.OK)// openFileDialog1不要再问我这是什么!
{
//获得文件路径
localFilePath = openFileDialog1.FileName.ToString();
}
AsposeExcel tt = new AsposeExcel(localFilePath);
DataTable dt;
try
{
dt = tt.ExcelToDatatalbe();
}
catch (Exception ex)
{
return;
}
//有了datatable你自己就可以DIY啦,下面是我自己的你不用理
if (ddlResidence.SelectedValue == "违章确认")
{
if (dt.Rows[0][9].ToString() != "违章确认")
{
return;
}
row = dt.Rows.Count;
if (row <= 0) return;
for (int i = 0; i < dt.Rows.Count; i++)
{
bllviola.Up_Confirmed_ByVnum(dt.Rows[i][6].ToString(), dt.Rows[i][9].ToString());
}
this.GridView1.DataSource = dt;
GridView1.DataBind();
}
C# WinForm使用Aspose.Cells.dll 导出导入Excel/Doc 完整实例教程的更多相关文章
- C# WinForm 导出导入Excel/Doc 完整实例教程[使用Aspose.Cells.dll]
[csharp] view plain copy 1.添加引用: Aspose.Cells.dll(我们就叫工具包吧,可以从网上下载.关于它的操作我在“Aspose.Cells操作说明 中文版 下载 ...
- C# winform 导出导入Excel/Doc 完整实例教程[网上看到的]
还真没做过winform的导出导入,今天上网百度了一下.结果--- 所以还是我自己写个吧.之前做过web的,半搬半做就OK. 1添加引用:Aspose.Cells.dll(我们就叫工具包吧,可以从网上 ...
- Aspose.Cells.dll引用导入导出Excel
Aspose.Cells 导入导出EXCEL 文章出处:http://hi.baidu.com/leilongbing/item/c11467e1819e5417595dd8c1 修改样式 ...
- C# 利用Aspose.Cells .dll将本地excel文档转化成pdf(完美破解版 无水印 无中文乱码)
Aspose.Cells .dll下载 http://pan.baidu.com/s/1slRENLF并引用 C#代码 using System; using System.Collections. ...
- 基于 Aspose.Cells与XML导入excel 数据----操作类封装
前言 导入excel数据, 在每个项目中基本上都会遇到,第三方插件或者基于微软office,用的最多的就是npoi,aspose.cells和c#基于office这三种方式,其中各有各的优缺点,在这也 ...
- C# 读写Excel的一些方法,Aspose.Cells.dll
需求:现有2个Excel,一个7000,一个20W,7000在20W是完全存在的.现要分离20W的,拆分成19W3和7000. 条件:两个Excel都有“登录名”,然后用“登录名”去关联2个Excel ...
- C# Aspose.Cells.dll Excel操作总结
简介 Aspose.Cells是一款功能强大的 Excel 文档处理和转换控件,不依赖 Microsoft Excel 环境,支持所有 Excel 格式类型的操作. 下载 Aspose.Cells.d ...
- Aspose.Cells.dll操作execl
附件:Aspose.Cells.dll 1.创建execl(不需要服务器或者客户端安装office) public void DCExexl(DataTable dt) { Workbook wb ...
- 利用Aspose.Cell控件导入Excel非强类型的数据
导入Excel的操作是非常常见的操作,可以使用Aspose.Cell.APOI.MyXls.OLEDB.Excel VBA等操作Excel文件,从而实现数据的导入,在导入数据的时候,如果是强类型的数据 ...
随机推荐
- IPC_共享内存
在IPC(InterProcess Communication)的通信模式下,不管是使用消息队列还是共享内存,甚至是信号量,每个IPC的对象(object)都有唯一的名字,称为“键”(key).通过“ ...
- 利用COPYDATASTRUCT传递命令行参数给驻留内存的进程(SendMessage应用)
我们知道Window进程之间通过API的SendMessage方法传递消息.但是方法参数lParam是Long型,那么如果传递一个字符串(譬如命令行参数)应该怎么办呢,甚至一个对象.结构呢.VB的发送 ...
- cocos2dx+lua中cc.EventListenerMouse:create()的bug
今天在调试项目的时候用到了鼠标事件的监听 在创建事件监听器的时候出了问题 cc.EventListenerMouse:create() 这句返回值为nil 原来这是cocos2dx引擎的一个bug,t ...
- XposedNoRebootModuleSample 不需要频繁重启调试的Xposed 模块源码例子
XposedNoRebootModuleSample(不需要频繁重启调试的Xposed 模块源码例子) Xposed Module Sample No Need To Reboot When Debu ...
- 高级正则表达式技术(Python版)
正则表达式是从信息中搜索特定的模式的一把瑞士军刀.它们是一个巨大的工具库,其中的一些功能经常被忽视或未被充分利用.今天我将向你们展示一些正则表达式的高级用法. 举个例子,这是一个我们可能用来检测电话美 ...
- XAML概览 1(译自JeremyBytes.com)
(文章译自JeremyBytes.com,由于原文太长,故分成几篇,能力所限,如有疏漏,希望海涵.另外若有侵权,务必尽快告知) Overview 了解XAML (可扩展应用程序标记语言)是使用WPF和 ...
- asp.net mvc 使用Ajax
使用asp.net mvc 调用Action方法很简单. 一.无参数方法. 1.首先,引入jquery-1.5.1.min.js 脚本,根据版本不同大家自行选择. <script src=&qu ...
- Java学习日志-01-Hello World
1.安装JDK1.7 2.安装eclipse 3.eclipse上写第一个java程序-hello world 先建工程,再建包,养成良好的习惯,然后新建类 若不先建立包,可能会提示"The ...
- [TL-WR841N V5~V9] 如何当作无线交换机使用?
http://service.tp-link.com.cn/detail_article_1034.html
- ios自定义View自动布局时计算大小
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/Impleme ...