c# winform
操作excel与操作数据库一样吗?也要连接、执行SQL语句吗?还是像txt文本,使用流数据streamwrite之类的?
怎样在窗体上显示excel文件内容。用什么控件显示?
如何向excel文件中写内容。

解决方案 »

  1.   

    参考代码,
     /// <summary>
            /// 转出Excel
            /// </summary>
            private void ToExcel()
            {
                try
                {
                    Excel.Application excel = new Excel.Application();
                    if (excel == null)
                    {
                        MessageBox.Show("无法创建Excel对象,可能您的机子未安装Excel");
                        return;
                    }
                    excel.Visible = false;
                    excel.Application.Workbooks.Add(true);                
                    excel.Cells[1, 1] = " 运  输  调   度   日  志";
                    
                    for (int i = 0; i < dgvBase.ColumnCount; i++)
                    {
                        excel.Cells[2, i + 1] = dgvBase.Columns[i].HeaderText;
                    }
                    for (int i = 0; i < dgvBase.RowCount - 1; i++)
                    {
                        for (int j = 0; j < dgvBase.ColumnCount; j++)
                        {
                            if (dgvBase[j, i].ValueType == typeof(string))
                            {
                                excel.Cells[i + 3, j + 1] = dgvBase[j, i].Value;
                            }
                            else
                            {
                                excel.Cells[i + 3, j + 1] = dgvBase[j, i].Value.ToString();
                            }
                        }
                    }
                    //设定单元格边框\列的宽度\字体、对齐方式等
                    excel.get_Range(excel.Cells[1, 1], excel.Cells[1, 18]).Font.Size = 16;//字体大小
                    excel.get_Range(excel.Cells[1, 1], excel.Cells[1, 18]).Font.Bold = true;//粗体
                    excel.get_Range(excel.Cells[1, 1], excel.Cells[1, 18]).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;//对齐方式
                    excel.get_Range(excel.Cells[2, 1], excel.Cells[dgvBase.RowCount + 1, dgvBase.ColumnCount]).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;                
                    excel.get_Range(excel.Cells[1, 1], excel.Cells[1, 18]).MergeCells = true;//合并单元格
                    excel.get_Range(excel.Cells[1, 1], excel.Cells[dgvBase.RowCount + 1, dgvBase.ColumnCount]).Borders.LineStyle = Excel.XlLineStyle.xlContinuous;//边框样式
                    excel.get_Range(excel.Cells[2, 3], excel.Cells[dgvBase.RowCount + 2, 3]).ColumnWidth = 13;//列宽
                    excel.get_Range(excel.Cells[2,9],excel.Cells[dgvBase.RowCount+2,9]).ColumnWidth =18;
                    excel.get_Range(excel.Cells[2,10],excel.Cells[dgvBase.RowCount+2,10]).ColumnWidth =18;
                    excel.get_Range(excel.Cells[2,12],excel.Cells[dgvBase.RowCount+2,12]).ColumnWidth =18;
                    excel.get_Range(excel.Cells[2, 15], excel.Cells[dgvBase.RowCount + 2, 15]).ColumnWidth = 11;
                    excel.Visible = true;
                    excel.Quit();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    
                    threadExcel.Abort();
                    //threadExcel.Join(5000);
                    //GC.Collect();            }
            }
      

  2.   

    string connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;IMEX=1;';Persist Security Info=False", "Excel的所在位置");
    string strCmd = string.Format("SELECT * FROM [{0}]", "sheetName");
    OleDbDataAdapter da = new OleDbDataAdapter(strCmd, connectionString);
    DataTable result = new DataTable();
    try
    {
    da.Fill(result);
    }
    catch (Exception err)
    {
    throw new ApplicationException("加载数据失败:" + err.Message, err);
    }
    da.Dispose();
    加载数据。写入的话需要用到excel的dll。根据里面的方法写入。
      

  3.   

    在网上搜一些这方面的资料 读取数据不想读取数据库文件因为没有对Excel提供像SQL那样的类库
      

  4.   

    OLEDB 驱动,和操作Acess类似或者直接使用Excel Applaction显示Excel?用DataView不就可以了?
      

  5.   

    读取Excel的时候,一般有两种:把Excel当作数据表来读;操作COM,用Excel的Range来读取
    写入Excel的时候,一般也有两种:操作COM,写Excel;用文件流来写入
      

  6.   

    读取的时候就和读取数据库里的数据是一样的,通过连接字符串来读取。
    写入的时候需要用到com, 调用安装Excel里的类来写入。调用方法,右键点项目引用,选择com里的microsoft excel 11 object library就自动添加到引用里,可以直接使用其中的类,来写入。