Excel 2003 (the effect is not ideal)
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>//TU//20150730 The effect is not ideal. </summary> public partial class Form2:form {string strfileurl = ""; <summary>/////</summary>/<returns></returns> DataSet setd ATA () {//create an emplyee datatable datatable employeetable = new DataTable ("Employee"); EMPLOYEETABLE.COLUMNS.ADD ("Employee ID"); EMPLOYEETABLE.COLUMNS.ADD ("Employee Name"); EMPLOYEETABLE.ROWS.ADD ("1", "Tu Ju Wen"); EMPLOYEETABLE.ROWS.ADD ("2", "GeoviNdu "); EMPLOYEETABLE.ROWS.ADD ("3", "Li 蘢 Yi"); 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 () {Initializec Omponent (); }//<summary>///</summary>//<param name= "SenDer "></param>//<param name=" E "></param> private void Form2_load (object sender, Event Args e) {this.dataGridView1.DataSource = SetData (). Tables[0]; }///<summary>//Excel 2003///TU///</summary>//<param name= "s Ender "></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 files (*.xlsx) |*.xlsx ";//| (*.xlsx) |*.xlsx Image Files (*. bmp;*. jpg;*. GIF) |*. bmp;*. jpg;*. Gif| AllFiles (*. *) |*.* txt files (*.txt) |*.txt| All Files (*. *) |*.* "openfiledialog1.filterindex = 2; Openfiledialog1.restoredirectory = true; if (openfiledialog1.showdialog () = = DialogResult.OK) {if (!openfiledialog1.filename.equ ALS (String.Empty)) {//reload purge data//this.combsheet.datasour CE = 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 the 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 ("Wrong Add File type"); }} else {MessageBox.Show ("you want to choose Select the exact location of the document "); }}} 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" ></para m>//<param name= "E" ></param> Private void Buttonexport_click (object sender, EventArgs e) {exporteasy (SetData (). Tables[0], "Ex.xls"); }///<summary>//export//</summary>//<param name= "Dtsource" ></param& Gt <param name= "strFileName" ></param> public static void Exporteasy (DataTable dtsource, String strfile Name) {try {xlsdocument xls = new xlsdocument (); Worksheet sheet = xls. WORKBOOK.WORKSHEETS.ADD ("Sheet1"); Fills the header of the foreach (DataColumn col in dtsource.columns) {sheet. Cells.add (1, Col. Ordinal + 1, col. ColumnName); }//Fill contents 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 ()); }}//Save XLS. FileName = strFileName; Xls. Save (); } catch (Exception ex) {ex. Message.tostring (); } } }}
Csharp:export or Import Excel using Myxls