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控件来触发打印操作,而难点 ...
随机推荐
- ORACLE 10g下载地址
ORACLE 10g下载地址 oracle 下载还需要用户名我自己注册了个方便大家使用下载 密码是这个 一般不会动了 大家也不用帮我找回密码了 每次都改 也很麻烦的如果有需要可以帮我浏览下 右侧的需要 ...
- T-SQL 小数点转换百分数
-- ============================================= -- Author: <Author,,CC> -- Create date: <C ...
- spring中@param和mybatis中@param使用差别
spring中@param /** * 查询指定用户和企业关联有没有配置角色 * @param businessId memberId * @return */ int selectRoleCount ...
- 不同iOS版本做代码适配__IPHONE_OS_VERSION_MAX_ALLOWED 和 __IPHONE_8_0等专业术语
目前开发只想最低版本支持iOS8了,iOS8以前的就不管了,然后现在iOS9和iOS10出来以后,有些新的API,也有些弃用的API,为了兼容,有时候代码里面需要编写判断不同iOS版本,或者只允许指定 ...
- 《Microsoft SQL Server 2008 Internals》读书笔记
http://www.cnblogs.com/downmoon/archive/2010/01/26/1656411.html
- 部署tomcat在windows服务器下,将tomcat控制台日志记录到日志文件中
在Linux系统中,Tomcat 启动后默认将很多信息都写入到 catalina.out 文件中,我们可以通过tail -f catalina.out 来跟踪Tomcat 和相关应用运行的情况. ...
- MVC + EF + Bootstrap 2 权限管理系统入门级(附源码)
MVC .EF 学习有大半年了,用的还不是很熟练,正好以做这样一个简单的权限管理系统作为学习的切入点,还是非常合适的. 开发环境: VS 2013 + Git + MVC 5 + EF 6 Code ...
- android dalvik heap 浅析
android 系统中可以在/system/build.prop中配置dalvik堆的有关设定.具体设定由如下三个属性来控制 -dalvik.vm.heapstartsize 堆分配的初始大小,调整 ...
- jQuery easyui combobox级联及内容联想
1.需求:已有一个下拉框A表示地区,现新增需求,需要在A选择不同地区时,增加一个展示该地区所有城市的下拉框B, 由于城市较多,要求B能实现用户输入和模糊匹配展示功能. 2.实现: (1)首先在A下面把 ...
- ruby include和exclude区别
很久没玩ruby了,今天看源码的时候,看到extend硬是缓不过神了,Google下extend和include的区别,做个记录 在class中include module, 那么module中的方法 ...