Preface
Computer room cooperation began a long time. Repeated re-opening several times, the project is also a drag-and-drop, as the leader. 80% of the responsibility lies with me. In order not to let this project accompany me to the Spring Festival. Ask yourself to be sure to knock it out in a few days.
Still the same problem, with C # Knock, from the beginning to now, no matter what a function of the implementation are now learning to investigate. This is not an unexpected feature for exporting Excel tables. On the internet to find a lot of information, anti-repetition to do a small demo, and finally have the effect I want.
Implement
The first is to add a reference
Then add the using on the program code
<span style= "FONT-SIZE:18PX;" >using system;using system.data;using system.windows.forms;using Excel = Microsoft.office.interop.excel;</span >
Add Datagriview controls to the window, add columns
You can then add the number of columns according to your needs, and you can edit the column headings.
Effect
Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">
Code implementation
<span style= "FONT-SIZE:18PX;" > private void Form2_load (object sender, EventArgs e) { dgvaccectcash.autogeneratecolumns = true; Dgvaccectcash.columns[0]. HeaderText = "study number"; DGVACCECTCASH.COLUMNS[1]. HeaderText = "Recharge Amount"; DGVACCECTCASH.COLUMNS[2]. HeaderText = "Recharge Time"; DGVACCECTCASH.COLUMNS[3]. HeaderText = "Recharge Date"; DGVACCECTCASH.COLUMNS[4]. HeaderText = "Operation user"; DGVACCECTCASH.COLUMNS[5]. HeaderText = "whether to checkout"; } </span>
Define two functions, one of which is to import the data from the DataGridView into the table. One is to export the data in the DataGridView to a single table.
1. One is to import the data from the DataGridView into the table
<span style= "FONT-SIZE:18PX;" > Private DataTable exporedatatotable (DataGridView DataGridView) {//import data from DataGridView into a table DataTable temptable = new DataTable ("TempTable"); Defines a template table specifically used to obtain the column name datatable modeltable = new DataTable ("Modeltable"); Create column for (int column = 0; column < DgvAccectCash.Columns.Count; column++) {//visible Columns shows if (Dgvaccectcash.columns[column]. Visible = = True) {DataColumn tempcolumn = new DataColumn (Dgvaccectcash.columns[column]. HeaderText, typeof (String)); TEMPTABLE.COLUMNS.ADD (Tempcolumn); DataColumn modelcolumn = new DataColumn (Dgvaccectcash.columns[column]. Name, typeof (String)); MODELTABLE.COLUMNS.ADD (Modelcolumn); }}//Join DataGridView Data to table for (int row = 0; row < DgvAccectCash.Rows.Count; row++) {if (Dgvaccectcash.rows[row]. Visible = = false) {continue; } DataRow Temprow = Temptable.newrow (); for (int i = 0; i < TempTable.Columns.Count; i++) {Temprow[i] = Dgvaccectcash.rows[r OW]. Cells[modeltable.columns[i]. ColumnName]. Value; } tempTable.Rows.Add (Temprow); } return temptable; }</span>
2. Export the data in DataGridView to a single table
<span style= "FONT-SIZE:18PX;" > private void Outputasexcelfile (DataGridView DataGridView) {//Exports data from DataGridView to a table D Atatable temptable = this.exporedatatotable (DataGridView); Export information to Excel table//Microsoft.Office.Interop.Excel.ApplicationClass myexcel; Microsoft.Office.Interop.Excel.ApplicationClass Myexcel; Microsoft.Office.Interop.Excel.Workbooks Myworkbooks; Microsoft.Office.Interop.Excel.Workbook MyWorkbook; Microsoft.Office.Interop.Excel.Worksheet Myworksheet; Char Mycolumns; Microsoft.Office.Interop.Excel.Range MyRange; object[,] myData = new object[500, 35]; int I, J;//J representative row, I for column Myexcel = new Microsoft.Office.Interop.Excel.ApplicationClass (); Show Excel myexcel.visible = true; if (Myexcel = = null) {MessageBox.Show ("Local Excel program cannot start!) Check that your Microsoft Office is properly installed and functioning correctly", "prompt"); Return } myworkbooks = Myexcel.workbooks; MyWorkbook = Myworkbooks.add (System.Reflection.Missing.Value); Myworksheet = (Microsoft.Office.Interop.Excel.Worksheet) myworkbook.worksheets[1]; Mycolumns = (char) (TempTable.Columns.Count + 64);//Set Column MyRange = Myworksheet.get_range ("A1", mycolumns.tostring () + "5");//Set column width int count = 0; Set column name foreach (DataColumn mynewcolumn in temptable.columns) {mydata[0, count] = Myn Ewcolumn.columnname; Count = count + 1; }//Output data records in DataGridView and placed in a two-dimensional array j = 1; foreach (DataRow myrow in temptable.rows)//Loop line {for (i = 0; i < TempTable.Columns.Count; i++ )//Loop column {mydata[j, i] = myrow[i]. ToString (); } j + +; }//Writes data from a two-dimensional array to excel MyRange =Myrange.get_resize (TempTable.Rows.Count + 1, tempTable.Columns.Count);//Create columns and rows myrange.value2 = MyData; MyRange.EntireColumn.AutoFit (); }</span>
Call these two functions to implement the export Excel table.
<span style= "FONT-SIZE:18PX;" > private void Button1_Click (object sender, EventArgs e) { exporedatatotable (dgvaccectcash);// Import data from DataGridView into a table outputasexcelfile (Dgvaccectcash);//Export data from DataGridView to a table }</span>
Effect
problem
There are still some questions:"Cannot embed Interop type" Microsoft.Office.Interop.Excel.ApplicationClass ".
Please use the applicable interface instead. Baidu is a bit because of the "Microsoft.Office.Interop.Excel" attribute in the reference, change the "Embed Interop type" true or False property is good.
Summary
Through this sample, I feel that I have grown a lot, although in the course of learning we will encounter a lot of problems. These problems look very difficult. But just to learn to stand on the shoulders of giants, to give ourselves a bit more patience and confidence that these difficulties will certainly become the cornerstone of our growth.
"Room Charge System C # Edition"--Export Excel