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的更多相关文章

  1. csharp: Export or Import excel using NPOI

    excel 2003: using System; using System.Collections.Generic; using System.ComponentModel; using Syste ...

  2. C#组件系列——又一款Excel处理神器Spire.XLS,你值得拥有(二)

    前言:上篇 C#组件系列——又一款Excel处理神器Spire.XLS,你值得拥有 介绍了下组件的两个功能,说不上特色,但确实能解决我们项目中的一些实际问题,这两天继续研究了下这个组件,觉得有些功能用 ...

  3. C#组件系列——又一款Excel处理神器Spire.XLS,你值得拥有

    前言:最近项目里面有一些对Excel操作的需求,博主想都没想,NPOI呗,简单.开源.免费,大家都喜欢!确实,对于一些简单的Excel导入.导出.合并单元格等,它都没啥太大的问题,但是这次的需求有两点 ...

  4. csharp: Export DataSet into Excel and import all the Excel sheets to DataSet

    /// <summary> /// Export DataSet into Excel /// </summary> /// <param name="send ...

  5. 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 ...

  6. 【原创】.NET读写Excel工具Spire.Xls使用(1)入门介绍

    在.NET平台,操作Excel文件是一个非常常用的需求,目前比较常规的方法有以下几种: 1.Office Com组件的方式:这个方式非常累人,微软的东西总是这么的复杂,使用起来可能非常不便,需要安装E ...

  7. .NET读写Excel工具Spire.Xls使用(1)入门介绍

    原文:[原创].NET读写Excel工具Spire.Xls使用(1)入门介绍 在.NET平台,操作Excel文件是一个非常常用的需求,目前比较常规的方法有以下几种: 1.Office Com组件的方式 ...

  8. Spire.XLS,生成Excel文件、加载Excel文件

    一.组件介绍 Spire.XLS是E-iceblue开发的一套基于企业级的专业Office文档处理的组件之一,全称Spire.Office for .NET.旗下有Spire.Doc,Spire XL ...

  9. c# winform打印excel(使用NPOI+Spire.xls+PrintDocument直接打印excel)

    前言 c#做winform程序要求生成并打印Excel报告,为了不安装Office相应组件,我选择了NPOI来生成Excel报告,用winform的PrintDocument控件来触发打印操作,而难点 ...

随机推荐

  1. 详细介绍windows下使用python pylot进行网站压力测试

    windows下使用python进行网站压力测试,有两个必不可少的程序需要安装,一个是python,另一个是pylot.python是一个安装软件,用来运行python程序,而pylot则是pytho ...

  2. [AX2012]Claims user

    AX2012可以创建一种account type为claims user的账号,这种账号不需要在AD中事先已创建用户,但是claims账号是无法通过rich client登陆到AX,它的主要应用场景是 ...

  3. Ubuntu 14.04 安装nVidia驱动后不能进入图形界面的恢复过程

    想要解决Ubuntu14.04的风扇不停的转的问题.由于ubuntu本身不支持双显卡切换,导致集显独显都处于开启状态,发热量和耗电量居高不下. 1. 安装驱动过程 参考[1]中的步骤,做了如下的操作. ...

  4. SQL语句 - 基本查询

    select select_list [ into new_table ] from table_source [ where search_condition ] [ group by broup_ ...

  5. CodeBlocks16.01+wxWidgets3.0.2

    [环境] windows7 x64,CodeBlocks16.01,wxWidgets3.0.2 [步骤] 一.下载所需的文件: 1.codeblocks官网:http://www.codeblock ...

  6. 关于Interface忽然想到的

    其实Interface,顾名思义,就是2个事物的"界面",interface衔接了2个事物,这两个事物,往往一方比较"多变",而另一方我们又不太希望它相对&qu ...

  7. UNIX环境高级编程笔记之进程控制

    本章重点介绍了进程控制的几个函数:fork.exec族._exit.wait和waitpid等,主要需要掌握的是父进程和子进程之间的运行机制,怎么处理进程的正常和异常终止.以及怎么让进程执行不同的程序 ...

  8. linux中mysql密码找回的两种方式

    方法一:修改my.cnf配置文件 1.首先确认服务器出于安全的状态,也就是没有人能够任意地连接MySQL数据库. 因为在重新设置MySQL的root密码的期间,MySQL数据库完全出于没有密码保护的  ...

  9. 再说 c++11 内存模型

    可见性与乱序 在说到内存模型相关的东西时,我们常常会说到两个名词:乱序与可见性,且两者经常交错着使用,容易给人错觉仿佛是两个不同的东西,其实不是这样,他们只是从不同的角度来描述一个事情,本质是相同的. ...

  10. cocos2d-x中使用可加密Sqlite存储玩家数据

    手机游戏当中的数据存储是一个重要的课题.cocos2d-x发展到现在的版本2.1.4,已经直接实现了对sqlite的支持(extensions/LocalStorage),这对我们一般的数据存储已经够 ...