csharp: Export or Import excel using MyXls,Spire.Xls
excel 2003 (效果不太理想)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using org.in2bits.MyXls;
using org.in2bits.MyXls.ByteUtil;
using System.IO;
using Directory = org.in2bits.MyOle2.Directory;
using NUnit.Framework;
using org.in2bits.MyOle2;
using System.Diagnostics; namespace MyxlsDemo
{ /// <summary>
/// 涂聚文
/// 20150730
/// 效果不太理想.
/// </summary>
public partial class Form2 : Form
{
string strFileUrl = "";
/// <summary>
///
/// </summary>
/// <returns></returns>
DataSet setData()
{
//Create an Emplyee DataTable
DataTable employeeTable = new DataTable("Employee");
employeeTable.Columns.Add("Employee ID");
employeeTable.Columns.Add("Employee Name");
employeeTable.Rows.Add("1", "涂聚文");
employeeTable.Rows.Add("2", "geovindu");
employeeTable.Rows.Add("3", "李蘢怡");
employeeTable.Rows.Add("4", "ноппчц");
employeeTable.Rows.Add("5", "ニヌネハヒフキカォноппчц");
//Create a Department Table
DataTable departmentTable = new DataTable("Department");
departmentTable.Columns.Add("Department ID");
departmentTable.Columns.Add("Department Name");
departmentTable.Rows.Add("1", "IT");
departmentTable.Rows.Add("2", "HR");
departmentTable.Rows.Add("3", "Finance"); //Create a DataSet with the existing DataTables
DataSet ds = new DataSet("Organization");
ds.Tables.Add(employeeTable);
ds.Tables.Add(departmentTable);
return ds; }
/// <summary>
///
/// </summary>
public Form2()
{
InitializeComponent();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form2_Load(object sender, EventArgs e)
{
this.dataGridView1.DataSource = setData().Tables[0];
}
/// <summary>
/// Excel 2003
/// 涂聚文
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnFile_Click(object sender, EventArgs e)
{
try
{
//bool imail = false;
this.Cursor = Cursors.WaitCursor;
openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
//JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif
openFileDialog1.Filter = "Excel 2000-2003 files(*.xls)|*.xls|Excel 2007 files (*.xlsx)|*.xlsx";//|(*.xlsx)|*.xlsx Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.* txt files (*.txt)|*.txt|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
if (!openFileDialog1.FileName.Equals(String.Empty))
{
//重新加载清除数据
//this.combSheet.DataSource = null;
//if (this.combSheet.Items.Count != 0)
//{
// this.combSheet.Items.Clear();
//}
FileInfo f = new FileInfo(openFileDialog1.FileName);
if (f.Extension.Equals(".xls") || f.Extension.Equals(".XLS") || f.Extension.Equals(".xlsx"))
{
this.Cursor = Cursors.WaitCursor;
strFileUrl = openFileDialog1.SafeFileName;
this.txtFileUrl.Text = openFileDialog1.FileName;
string currentfilename = openFileDialog1.FileName;
this.txtFileUrl.Text = currentfilename;
XlsDocument xls = new XlsDocument(currentfilename);
DataTable com = new DataTable();
com.Columns.Add("id", typeof(int));
com.Columns.Add("name", typeof(string));
// xls.FileName = currentfilename;
for(int id = 0; id < xls.Workbook.Worksheets.Count; id++)
{
com.Rows.Add(id,xls.Workbook.Worksheets[id].Name);
}
this.combSheet.DataSource = com;
this.combSheet.DisplayMember = "name";
this.combSheet.ValueMember = "id";
Worksheet sheet = xls.Workbook.Worksheets[0];
DataTable dt = new DataTable();
//xls.Workbook.Worksheets[0].Name.ToString();
int i = 0;
int FirstRow = (int)sheet.Rows.MinRow;
if (i == 0)
{
//write data in every cell in the first row in the first worksheet as the column header(note: in order to write data from xls document in DataTable)
for (int j = 1; j < sheet.Rows[1].CellCount + 1; j++)
{
string ColumnName = Convert.ToString(sheet.Rows[1].GetCell(ushort.Parse(j.ToString())).Value);
DataColumn column = new DataColumn(ColumnName);
dt.Columns.Add(column); }
FirstRow++;
}
// write data(not including column header) in datatable rows in sequence
for (int k = FirstRow; k < sheet.Rows.MaxRow + 1; k++)
{
Row row = sheet.Rows[ushort.Parse(k.ToString())];
DataRow dataRow = dt.NewRow();
for (int z = 1; z < sheet.Rows[ushort.Parse(k.ToString())].CellCount + 1; z++)
{
// write data in the current cell if it exists
if (row.GetCell(ushort.Parse(z.ToString())) != null)
{
dataRow[z - 1] = row.GetCell(ushort.Parse(z.ToString())).Value.ToString();
} }
dt.Rows.Add(dataRow);
} this.dataGridView1.DataSource = dt; this.Cursor = Cursors.Default;
}
else
{
MessageBox.Show("错添文件类型");
}
}
else
{
MessageBox.Show("你要选择一下精确位置的文件");
} }
}
catch (Exception ex)
{
ex.Message.ToString();
} this.Cursor = Cursors.Default;
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnImport_Click(object sender, EventArgs e)
{ }
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonExport_Click(object sender, EventArgs e)
{
ExportEasy(setData().Tables[0], "ex.xls");
} /// <summary>
/// 导出
/// </summary>
/// <param name="dtSource"></param>
/// <param name="strFileName"></param>
public static void ExportEasy(DataTable dtSource, string strFileName)
{
try
{
XlsDocument xls = new XlsDocument();
Worksheet sheet = xls.Workbook.Worksheets.Add("Sheet1");
//填充表头
foreach (DataColumn col in dtSource.Columns)
{
sheet.Cells.Add(1, col.Ordinal + 1, col.ColumnName);
} //填充内容
for (int i = 0; i < dtSource.Rows.Count; i++)
{
for (int j = 0; j < dtSource.Columns.Count; j++)
{
sheet.Cells.Add(i + 2, j + 1, dtSource.Rows[i][j].ToString());
}
} //保存
xls.FileName = strFileName;
xls.Save();
}
catch (Exception ex)
{
ex.Message.ToString();
}
}
}
}
Spire.Xls
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Spire.Xls; namespace SpireXlsDemo
{
public partial class Form1 : Form
{ /// <summary>
///
/// </summary>
public Form1()
{
InitializeComponent();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{ try
{
string file1 = Environment.CurrentDirectory + @"\2015073001.xls";
string file2 = Environment.CurrentDirectory + @"\2015073002.xls";
Workbook workbook = new Workbook();
//load the first workbook
workbook.LoadFromFile(@"2015073001.xls");
//load the second workbook
Workbook workbook2 = new Workbook();
workbook2.LoadFromFile(@"2015073002.xls"); //import the second workbook's worksheet into the first workbook using a datatable
Worksheet sheet2 = workbook2.Worksheets[0];
DataTable dataTable = sheet2.ExportDataTable();
Worksheet sheet1 = workbook.Worksheets[0];
sheet1.InsertDataTable(dataTable, false, sheet1.LastRow + 1, 1); //save the workbook
workbook.SaveToFile("result.xls");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
}
csharp: Export or Import excel using MyXls,Spire.Xls的更多相关文章
- csharp: Export or Import excel using NPOI
excel 2003: using System; using System.Collections.Generic; using System.ComponentModel; using Syste ...
- C#组件系列——又一款Excel处理神器Spire.XLS,你值得拥有(二)
前言:上篇 C#组件系列——又一款Excel处理神器Spire.XLS,你值得拥有 介绍了下组件的两个功能,说不上特色,但确实能解决我们项目中的一些实际问题,这两天继续研究了下这个组件,觉得有些功能用 ...
- C#组件系列——又一款Excel处理神器Spire.XLS,你值得拥有
前言:最近项目里面有一些对Excel操作的需求,博主想都没想,NPOI呗,简单.开源.免费,大家都喜欢!确实,对于一些简单的Excel导入.导出.合并单元格等,它都没啥太大的问题,但是这次的需求有两点 ...
- csharp: Export DataSet into Excel and import all the Excel sheets to DataSet
/// <summary> /// Export DataSet into Excel /// </summary> /// <param name="send ...
- csharp: Export DataTable to Excel using OpenXml 2.5 in asp.net
//https://www.microsoft.com/en-us/download/details.aspx?id=5124 Open XML SDK 2.0 for Microsoft Offic ...
- 【原创】.NET读写Excel工具Spire.Xls使用(1)入门介绍
在.NET平台,操作Excel文件是一个非常常用的需求,目前比较常规的方法有以下几种: 1.Office Com组件的方式:这个方式非常累人,微软的东西总是这么的复杂,使用起来可能非常不便,需要安装E ...
- .NET读写Excel工具Spire.Xls使用(1)入门介绍
原文:[原创].NET读写Excel工具Spire.Xls使用(1)入门介绍 在.NET平台,操作Excel文件是一个非常常用的需求,目前比较常规的方法有以下几种: 1.Office Com组件的方式 ...
- Spire.XLS,生成Excel文件、加载Excel文件
一.组件介绍 Spire.XLS是E-iceblue开发的一套基于企业级的专业Office文档处理的组件之一,全称Spire.Office for .NET.旗下有Spire.Doc,Spire XL ...
- c# winform打印excel(使用NPOI+Spire.xls+PrintDocument直接打印excel)
前言 c#做winform程序要求生成并打印Excel报告,为了不安装Office相应组件,我选择了NPOI来生成Excel报告,用winform的PrintDocument控件来触发打印操作,而难点 ...
随机推荐
- [Unit Testing] AngularJS Unit Testing - Karma
Install Karam: npm install -g karma npm install -g karma-cli Init Karam: karma init First test: 1. A ...
- Jenkins FTP 上传
需要插件:FTP publisher plugin 进入 Jenkins / 系统管理 / 系统设置 找到 FTP repository hosts,新增一个,编辑好,保存 打开 Jenkins / ...
- 自定义圆环progressbar
RoundProgressBar.java /** * RoundProgressBar.java [v1.0.0] * classes: com.example.audiorecordingtest ...
- TN035: Using Multiple Resource Files and Header Files with Visual C++
TN035: Using Multiple Resource Files and Header Files with Visual C++ This note describes how the Vi ...
- C# WinForm RDLC报表不预览直接连续打印
用微软的RDLC报表直接打印不预览 直接上代码. //打印清单 System.Data.DataTable dt = print_QD(dr); ReportViewer rvDoc = new Re ...
- Socket模型详解(转)
两种I/O模式 一.选择模型 二.异步选择 三.事件选择 四.重叠I/O模型 五.完成端口模型 五种I/O模型的比较 两种I/O模式 1. 两种I/O模式 阻塞模式:执行I/O操作完成前会一直进行等待 ...
- NetBPM的安装 -转
NetBPM的安装还是比较简单的,有比较详细的文档. 1.当然是先下载运行程序了, netbpm-0.8.3.1.zip ,官方网站:http://www.netbpm.org:2.然后解压后自己看 ...
- Android Launcher 研究学习
Launcher是系统启动后第一个启动的程序,是其它应用程序的入口,也就是我们的手机程序的桌面程序; 一.Launcher的定义及构成: <1>通过查看官方提供的Launcher源码可以知 ...
- tcp为什么需要3次握手4次挥手
一.起因 在网络请求中,为了提升性能,通常会采用长连接的方式避免在每一次交互都进行网络链接的创建和关闭,而长连接就是tpc的链接方式.因而有必要对tcp的创建链接和关闭有所了解.在网络上查询了一些知识 ...
- ionic介绍
ionic介绍 Ionic是一个前端的框架,帮助开发者使用HTML5, CSS3和JavaScript做出原生应用. The beautiful, open source front-end fram ...