• DataGrid1 和 EXCEL2003 间的相互操作 (DataGrid1 中是什么就向EXCEL中导出什么,隐藏列的不导出)


     //导出到EXCEL
            public  bool toOutExcel(DataGridView dg, string fileName)
            {
                SaveFileDialog dlg = new SaveFileDialog();
                dlg.Filter = "Excel files (*.xls)|*.xls|All files (*.*)|*.*";
                dlg.FilterIndex = 1;
                dlg.DefaultExt = "xls";
                dlg.FileName = fileName + ".xls";
                if (dlg.ShowDialog() != DialogResult.OK) return false;
                try
                {
                    Excel.Application m_objExcel = new Excel.Application();
                    if (m_objExcel == null)
                    {
                        MessageBox.Show("EXCEL无法启动!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                    m_objExcel.Visible = true;
                    Excel.Workbooks m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
                    Excel._Workbook m_objBook = (Excel._Workbook)(m_objBooks.Add(Type.Missing));
                    Excel.Sheets m_objSheets = (Excel.Sheets)m_objBook.Worksheets;
                    Excel._Worksheet m_objSheet = (Excel._Worksheet)(m_objSheets.get_Item(1));

                    //设置格式
                    //自由列宽
                    Excel.Range m_objRange = m_objSheet.get_Range("A1", Type.Missing);
                    m_objRange = m_objRange.get_Resize(dg.RowCount + 1, dg.ColumnCount);
                    m_objRange.HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;
                    m_objRange.Font.Size = 10;
                    m_objRange.NumberFormatLocal = "@";


                    for (int icol = 0; icol < dg.ColumnCount; icol++)
                    {
                        DataGridViewColumn dgc = dg.Columns[icol];
                        if (dgc.Visible == false)
                        {
                            dg.Columns.Remove(dgc);
                            icol--;
                        }
                    }
                    object[,] objData = new Object[dg.RowCount + 1, dg.ColumnCount];
                    for (int r = 0; r < dg.RowCount + 1; r++)
                    {
                        for (int j = 0; j < dg.ColumnCount; j++)
                        {
                            if (r == 0)//加标题
                            {
                                objData[r, j] = dg.Columns[j].HeaderText;
                            }
                            else
                            {
                                objData[r, j] = dg[j, r - 1].Value;
                            }
                        }
                    }
                    m_objRange.Value2 = objData;
                    m_objRange.Columns.AutoFit();

                     object m_objOpt = System.Reflection.Missing.Value;
                    if (System.IO.File.Exists(dlg.FileName))
                    {
                        System.IO.File.Delete(dlg.FileName);
                    }

                    m_objBook.SaveAs(dlg.FileName, m_objOpt, m_objOpt,
                                     m_objOpt, m_objOpt, m_objOpt, Excel.XlSaveAsAccessMode.xlNoChange,
                                     m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt);

                    return true;
                }
                catch (Exception ex)
                {
                    Utility.ShowErrorMessage(ex.Message);
                    return false;
                }
            }

            //由 EXCEL 导入到DataGrid1 EXCEL2003
            public void toDgvFromExcel(DataGridView dg)
            {
                // 打开文件
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.Filter = "Excel files (*.xls)|*.xls|All files (*.*)|*.*";
                dlg.FilterIndex = 1;
                dlg.DefaultExt = "xls";
                dlg.FileName = "端子排数据.xls";
                if (dlg.ShowDialog() != DialogResult.OK)
                    return  ;

                System.Data.OleDb.OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection();
                System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1 = new System.Data.OleDb.OleDbDataAdapter();
                DataSet dataSet1 = new DataSet();
                string strConnection = "";
                strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                    "Data Source=" + dlg.FileName + ";" +
                    "Extended Properties=Excel 8.0;";
                oleDbConnection1.ConnectionString = strConnection;
                oleDbDataAdapter1.SelectCommand = new System.Data.OleDb.OleDbCommand();
                oleDbDataAdapter1.SelectCommand.CommandText = "SELECT * FROM [Sheet1$]";
                oleDbDataAdapter1.SelectCommand.Connection = oleDbConnection1;
                DataTable dt = new DataTable();
                oleDbDataAdapter1.Fill(dt);
                dg.DataSource = dt;
            }

  • 相关阅读:
    安卓-登陆页面的实现
    异常
    实用类
    Hashset
    Map
    LinkedList
    arraylist
    继承
    字符串相关代码
    数组代码
  • 原文地址:https://www.cnblogs.com/houlinbo/p/1601292.html
Copyright © 2020-2023  润新知