excel 2003 (效果不太理想)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using org.in2bits.MyXls;
  10. using org.in2bits.MyXls.ByteUtil;
  11. using System.IO;
  12. using Directory = org.in2bits.MyOle2.Directory;
  13. using NUnit.Framework;
  14. using org.in2bits.MyOle2;
  15. using System.Diagnostics;
  16.  
  17. namespace MyxlsDemo
  18. {
  19.  
  20. /// <summary>
  21. /// 涂聚文
  22. /// 20150730
  23. /// 效果不太理想.
  24. /// </summary>
  25. public partial class Form2 : Form
  26. {
  27. string strFileUrl = "";
  28. /// <summary>
  29. ///
  30. /// </summary>
  31. /// <returns></returns>
  32. DataSet setData()
  33. {
  34. //Create an Emplyee DataTable
  35. DataTable employeeTable = new DataTable("Employee");
  36. employeeTable.Columns.Add("Employee ID");
  37. employeeTable.Columns.Add("Employee Name");
  38. employeeTable.Rows.Add("1", "涂聚文");
  39. employeeTable.Rows.Add("2", "geovindu");
  40. employeeTable.Rows.Add("3", "李蘢怡");
  41. employeeTable.Rows.Add("4", "ноппчц");
  42. employeeTable.Rows.Add("5", "ニヌネハヒフキカォноппчц");
  43. //Create a Department Table
  44. DataTable departmentTable = new DataTable("Department");
  45. departmentTable.Columns.Add("Department ID");
  46. departmentTable.Columns.Add("Department Name");
  47. departmentTable.Rows.Add("1", "IT");
  48. departmentTable.Rows.Add("2", "HR");
  49. departmentTable.Rows.Add("3", "Finance");
  50.  
  51. //Create a DataSet with the existing DataTables
  52. DataSet ds = new DataSet("Organization");
  53. ds.Tables.Add(employeeTable);
  54. ds.Tables.Add(departmentTable);
  55. return ds;
  56.  
  57. }
  58. /// <summary>
  59. ///
  60. /// </summary>
  61. public Form2()
  62. {
  63. InitializeComponent();
  64. }
  65. /// <summary>
  66. ///
  67. /// </summary>
  68. /// <param name="sender"></param>
  69. /// <param name="e"></param>
  70. private void Form2_Load(object sender, EventArgs e)
  71. {
  72. this.dataGridView1.DataSource = setData().Tables[0];
  73. }
  74. /// <summary>
  75. /// Excel 2003
  76. /// 涂聚文
  77. /// </summary>
  78. /// <param name="sender"></param>
  79. /// <param name="e"></param>
  80. private void btnFile_Click(object sender, EventArgs e)
  81. {
  82. try
  83. {
  84. //bool imail = false;
  85. this.Cursor = Cursors.WaitCursor;
  86. openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
  87. //JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif
  88. 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 (*.*)|*.*"
  89. openFileDialog1.FilterIndex = 2;
  90. openFileDialog1.RestoreDirectory = true;
  91. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  92. {
  93. if (!openFileDialog1.FileName.Equals(String.Empty))
  94. {
  95. //重新加载清除数据
  96. //this.combSheet.DataSource = null;
  97. //if (this.combSheet.Items.Count != 0)
  98. //{
  99. // this.combSheet.Items.Clear();
  100. //}
  101. FileInfo f = new FileInfo(openFileDialog1.FileName);
  102. if (f.Extension.Equals(".xls") || f.Extension.Equals(".XLS") || f.Extension.Equals(".xlsx"))
  103. {
  104. this.Cursor = Cursors.WaitCursor;
  105. strFileUrl = openFileDialog1.SafeFileName;
  106. this.txtFileUrl.Text = openFileDialog1.FileName;
  107. string currentfilename = openFileDialog1.FileName;
  108. this.txtFileUrl.Text = currentfilename;
  109. XlsDocument xls = new XlsDocument(currentfilename);
  110. DataTable com = new DataTable();
  111. com.Columns.Add("id", typeof(int));
  112. com.Columns.Add("name", typeof(string));
  113. // xls.FileName = currentfilename;
  114. for(int id = 0; id < xls.Workbook.Worksheets.Count; id++)
  115. {
  116. com.Rows.Add(id,xls.Workbook.Worksheets[id].Name);
  117. }
  118. this.combSheet.DataSource = com;
  119. this.combSheet.DisplayMember = "name";
  120. this.combSheet.ValueMember = "id";
  121. Worksheet sheet = xls.Workbook.Worksheets[0];
  122. DataTable dt = new DataTable();
  123. //xls.Workbook.Worksheets[0].Name.ToString();
  124. int i = 0;
  125. int FirstRow = (int)sheet.Rows.MinRow;
  126. if (i == 0)
  127. {
  128. //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)
  129. for (int j = 1; j < sheet.Rows[1].CellCount + 1; j++)
  130. {
  131. string ColumnName = Convert.ToString(sheet.Rows[1].GetCell(ushort.Parse(j.ToString())).Value);
  132. DataColumn column = new DataColumn(ColumnName);
  133. dt.Columns.Add(column);
  134.  
  135. }
  136. FirstRow++;
  137. }
  138. // write data(not including column header) in datatable rows in sequence
  139. for (int k = FirstRow; k < sheet.Rows.MaxRow + 1; k++)
  140. {
  141. Row row = sheet.Rows[ushort.Parse(k.ToString())];
  142. DataRow dataRow = dt.NewRow();
  143. for (int z = 1; z < sheet.Rows[ushort.Parse(k.ToString())].CellCount + 1; z++)
  144. {
  145. // write data in the current cell if it exists
  146. if (row.GetCell(ushort.Parse(z.ToString())) != null)
  147. {
  148. dataRow[z - 1] = row.GetCell(ushort.Parse(z.ToString())).Value.ToString();
  149. }
  150.  
  151. }
  152. dt.Rows.Add(dataRow);
  153. }
  154.  
  155. this.dataGridView1.DataSource = dt;
  156.  
  157. this.Cursor = Cursors.Default;
  158. }
  159. else
  160. {
  161. MessageBox.Show("错添文件类型");
  162. }
  163. }
  164. else
  165. {
  166. MessageBox.Show("你要选择一下精确位置的文件");
  167. }
  168.  
  169. }
  170. }
  171. catch (Exception ex)
  172. {
  173. ex.Message.ToString();
  174. }
  175.  
  176. this.Cursor = Cursors.Default;
  177. }
  178. /// <summary>
  179. ///
  180. /// </summary>
  181. /// <param name="sender"></param>
  182. /// <param name="e"></param>
  183. private void btnImport_Click(object sender, EventArgs e)
  184. {
  185.  
  186. }
  187. /// <summary>
  188. ///
  189. /// </summary>
  190. /// <param name="sender"></param>
  191. /// <param name="e"></param>
  192. private void buttonExport_Click(object sender, EventArgs e)
  193. {
  194. ExportEasy(setData().Tables[0], "ex.xls");
  195. }
  196.  
  197. /// <summary>
  198. /// 导出
  199. /// </summary>
  200. /// <param name="dtSource"></param>
  201. /// <param name="strFileName"></param>
  202. public static void ExportEasy(DataTable dtSource, string strFileName)
  203. {
  204. try
  205. {
  206. XlsDocument xls = new XlsDocument();
  207. Worksheet sheet = xls.Workbook.Worksheets.Add("Sheet1");
  208. //填充表头
  209. foreach (DataColumn col in dtSource.Columns)
  210. {
  211. sheet.Cells.Add(1, col.Ordinal + 1, col.ColumnName);
  212. }
  213.  
  214. //填充内容
  215. for (int i = 0; i < dtSource.Rows.Count; i++)
  216. {
  217. for (int j = 0; j < dtSource.Columns.Count; j++)
  218. {
  219. sheet.Cells.Add(i + 2, j + 1, dtSource.Rows[i][j].ToString());
  220. }
  221. }
  222.  
  223. //保存
  224. xls.FileName = strFileName;
  225. xls.Save();
  226. }
  227. catch (Exception ex)
  228. {
  229. ex.Message.ToString();
  230. }
  231. }
  232. }
  233. }

  Spire.Xls

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using Spire.Xls;
  10.  
  11. namespace SpireXlsDemo
  12. {
  13. public partial class Form1 : Form
  14. {
  15.  
  16. /// <summary>
  17. ///
  18. /// </summary>
  19. public Form1()
  20. {
  21. InitializeComponent();
  22. }
  23. /// <summary>
  24. ///
  25. /// </summary>
  26. /// <param name="sender"></param>
  27. /// <param name="e"></param>
  28. private void Form1_Load(object sender, EventArgs e)
  29. {
  30.  
  31. try
  32. {
  33. string file1 = Environment.CurrentDirectory + @"\2015073001.xls";
  34. string file2 = Environment.CurrentDirectory + @"\2015073002.xls";
  35. Workbook workbook = new Workbook();
  36. //load the first workbook
  37. workbook.LoadFromFile(@"2015073001.xls");
  38. //load the second workbook
  39. Workbook workbook2 = new Workbook();
  40. workbook2.LoadFromFile(@"2015073002.xls");
  41.  
  42. //import the second workbook's worksheet into the first workbook using a datatable
  43. Worksheet sheet2 = workbook2.Worksheets[0];
  44. DataTable dataTable = sheet2.ExportDataTable();
  45. Worksheet sheet1 = workbook.Worksheets[0];
  46. sheet1.InsertDataTable(dataTable, false, sheet1.LastRow + 1, 1);
  47.  
  48. //save the workbook
  49. workbook.SaveToFile("result.xls");
  50. }
  51. catch (Exception ex)
  52. {
  53. MessageBox.Show(ex.Message.ToString());
  54. }
  55. }
  56. }
  57. }

  

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. ORACLE 10g下载地址

    ORACLE 10g下载地址 oracle 下载还需要用户名我自己注册了个方便大家使用下载 密码是这个 一般不会动了 大家也不用帮我找回密码了 每次都改 也很麻烦的如果有需要可以帮我浏览下 右侧的需要 ...

  2. T-SQL 小数点转换百分数

    -- ============================================= -- Author: <Author,,CC> -- Create date: <C ...

  3. spring中@param和mybatis中@param使用差别

    spring中@param /** * 查询指定用户和企业关联有没有配置角色 * @param businessId memberId * @return */ int selectRoleCount ...

  4. 不同iOS版本做代码适配__IPHONE_OS_VERSION_MAX_ALLOWED 和 __IPHONE_8_0等专业术语

    目前开发只想最低版本支持iOS8了,iOS8以前的就不管了,然后现在iOS9和iOS10出来以后,有些新的API,也有些弃用的API,为了兼容,有时候代码里面需要编写判断不同iOS版本,或者只允许指定 ...

  5. 《Microsoft SQL Server 2008 Internals》读书笔记

    http://www.cnblogs.com/downmoon/archive/2010/01/26/1656411.html

  6. 部署tomcat在windows服务器下,将tomcat控制台日志记录到日志文件中

    在Linux系统中,Tomcat 启动后默认将很多信息都写入到 catalina.out 文件中,我们可以通过tail  -f  catalina.out 来跟踪Tomcat 和相关应用运行的情况. ...

  7. MVC + EF + Bootstrap 2 权限管理系统入门级(附源码)

    MVC .EF 学习有大半年了,用的还不是很熟练,正好以做这样一个简单的权限管理系统作为学习的切入点,还是非常合适的. 开发环境: VS 2013 + Git + MVC 5 + EF 6 Code ...

  8. android dalvik heap 浅析

    android 系统中可以在/system/build.prop中配置dalvik堆的有关设定.具体设定由如下三个属性来控制 -dalvik.vm.heapstartsize  堆分配的初始大小,调整 ...

  9. jQuery easyui combobox级联及内容联想

    1.需求:已有一个下拉框A表示地区,现新增需求,需要在A选择不同地区时,增加一个展示该地区所有城市的下拉框B, 由于城市较多,要求B能实现用户输入和模糊匹配展示功能. 2.实现: (1)首先在A下面把 ...

  10. ruby include和exclude区别

    很久没玩ruby了,今天看源码的时候,看到extend硬是缓不过神了,Google下extend和include的区别,做个记录 在class中include module, 那么module中的方法 ...