C# winform 导出导入Excel/Doc 完整实例教程 使用Aspose.Cells.dll——第一篇
来源:互联网 发布:58和房天下端口那个好 编辑:程序博客网 时间:2024/05/16 09:03
C# winform 导出导入Excel/Doc 完整实例教程 使用Aspose.Cells.dll——第一篇
http://hi.baidu.com/%BA%CE%B3%B1/blog/item/3298ae512dd15a2f42a75b1a.html
还真没做过winform的导出导入,今天上网百度了一下。结果---
所以还是我自己写个吧。之前做过web的,半搬半做就OK。
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;
}
}
3导出按钮事件中编写导出数据方法:
private void bt_excel_out_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();//如果你直接从对话框中拉出来,就不用new了噢。
//设置文件类型
saveFileDialog1.Filter = "导出Excel (*.xls)|*.xls|Word (*.doc)|*.doc";
saveFileDialog1.FilterIndex = 2;
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 = FilePath + "//" + newFileName;
System.IO.FileStream fs = (System.IO.FileStream)saveFileDialog1.OpenFile();//输出文件
//下面是导出数据到doc 导出到excel请看下一篇(发现没有AsposeExcel
类还没有用到呢,那正是导出到excel要用的)
StreamWriter writer = new StreamWriter(fs);
writer.Write("tttt");//这里就是你要导出到word文档的数据
writer.Flush();
writer.Close();
fs.Close();
}
}
迟的我会上传一份全面的 C# winform教程,若有需要留意下噢。
- C# winform 导出导入Excel/Doc 完整实例教程 使用Aspose.Cells.dll——第一篇
- C# winform 导出导入Excel/Doc 完整实例教程 使用Aspose.Cells.dll——第一篇
- C# winform 导出导入Excel/Doc 完整实例教程 使用Aspose.Cells.dll——第二篇
- C# winform 导出导入Excel/Doc 完整实例教程 使用Aspose.Cells.dll——第三篇
- C# WinForm 导出导入Excel/Doc 完整实例教程[使用Aspose.Cells.dll]
- C# WinForm 导出导入Excel/Doc 完整实例教程[使用Aspose.Cells.dll]
- C# WinForm 导出导入Excel/Doc 完整实例教程[使用Aspose.Cells.dll]
- C# WinForm 导出导入Excel/Doc 完整实例教程
- Aspose.Cells Excel导出导入
- WinForm使用Aspose.cells利用模板导出Excel
- 使用Aspose.Cells.dll导出数据到Excel
- 【学习笔记】C#使用Aspose.Cells.dll处理excel表格
- c#使用aspose.cells 从datatable导出数据到excel
- c#使用aspose.cells 从datatable导出数据到excel
- C# 使用 C# Aspose.Cells导出多个Sheet的Excel .net导出Excel C#导出Excel
- Aspose.Excel.dll Excel导入、导出
- MVC利用Aspose.Cells.dll实现Excel数据导入数据库
- (C#)利用Aspose.Cells组件导入导出excel文件
- 面试很重要的几点【看到很好,所以就自己留下了,哈哈】
- 【经典】C#winform从数据集导出Excel(带指定Excel样式) 转
- 闷了我一棍:人之患在好为人师
- 基本编码规则(BER)介绍--SNMP
- C# winform 导出导入Excel/Doc 完整实例教程 使用Aspose.Cells.dll——第二篇
- C# winform 导出导入Excel/Doc 完整实例教程 使用Aspose.Cells.dll——第一篇
- ASP.NET的global.asax文件中的Application事件(二)
- C# winform 导出导入Excel/Doc 完整实例教程 使用Aspose.Cells.dll——第三篇
- Windows 下 SVN 服务器配置
- 将excel文件导入到winform的控件中
- 关于在WinForm下对Excel进行操作的总结
- 实现c#中winform窗体嵌入并操作可视化Excel表格
- SIP中3个很重要的概念,就是dialog, session和transaction
- 80后不为人知的寂莫