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. 告诉你吧,一套皮肤在winform与wpf开发模式下实现的界面效果同样精彩,winform界面和wpf界面。

    一.同一资源: 二.先上软件界面: (1)wpf界面: 在wpf中实现这样类似web风格的软件界面就不用我多说了,在wpf实现这样的风格是很简单的,完全像网页设计一样的. (2)winform界面 在 ...

  2. text .global_start 和_start

    .text定义一个代码段,这是GCC必须的.global_start和_start代码开始地址定义个标记

  3. iOS 基础复习

    silverlight知识点:linqToSQL.视图.存储过程.索引.触发器 数据结构:数组.栈.队列.链表.属.图. 排序算法:插入.选择.交换(冒泡).归并 网络开发:HTTP短连接.socke ...

  4. 做最好的日期控件,My97 DatePicker 4.8 Beta4

    4.8更新的内容 [新增]preload预载选项 [新增]时分秒选择菜单的定制功能,详见hmsMenuCfg属性[beta4] [新增]$dp.unbind函数,允许用户手动接触日期控件绑定[beta ...

  5. 自动SPF生成工具

    到openspf网站去自动生成一下,地址是http://old.openspf.org/wizard.html.详细解释见下图关于spf的详细语法请看http://www.openspf.org/SP ...

  6. 使用Merge Into 语句实现 Insert/Update

    网址: http://www.eygle.com/digest/2009/01/merge_into_insertupdate.html 动机: 想在Oracle中用一条SQL语句直接进行Insert ...

  7. HTML5大数据可视化效果(二)可交互地铁线路图

    前言 最近特别忙,承蒙大伙关照,3D机房的项目一个接着一个,领了一帮小弟,搞搞传帮带,乌飞兔走,转眼已经菊黄蟹肥……有个小弟很不错,勤奋好学,很快就把API都摸透了,自己折腾着做了个HTML5的魔都的 ...

  8. Turtle Online:致力于打造超接地气的PC前端架构,组件+API,快速搭建前端开发

    架构创作初衷 每当新开一个项目时,都会绞尽脑汁去考虑采用哪种框架:requirejs/seajs.jquery/zepto.backbone.easeUI/Bootstrap/AngularJS……, ...

  9. Win10系统下编译GDAL1.9.2版本

    环境说明: 1.Win10企业版.64位: 2.VS2012旗舰版: 3.GDAL1.9.2 GADL编译 1.解压GDAL压缩包至F:\GDAL\gdal-1.9.2: 2.设置GDAL编译后安装目 ...

  10. MeshCombineUtility.cs method `GetTriangleStrip' of type `UnityEngine.Mesh' could be found

    1) Assets/Standard Assets/Scripts/MeshCombineUtility.cs(27,74): error CS1061: Type `UnityEngine.Mesh ...