关于#c语言#的问题,请各位专家解答!

跪求c语言读写excel数据的代码,求求了!

visual studio下可以直接去市场搜索,有现成的库
比如Aspose.Cells,还有其他很多记不清名字了
另外如果目标环境装了OFFICE的话也可以直接调用office接口

代码给你需要引用excel的两个dll

 public static void excelshow()
        {
            string connString;
            string path = Path.GetFileNameWithoutExtension(strPath);
            string fileSuffix = System.IO.Path.GetExtension(path);
            //string FIL = path.Substring(path.LastIndexOf(".") + 1, (path.Length - path.LastIndexOf(".") - 1));
            try
            {
                if (fileSuffix == ".xls")
                {
                    connString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strPath + ";" + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
                }
                else
                {
                    connString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + strPath + ";" + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\"";
                }
                OleDbConnection con = new OleDbConnection(connString);
                string strSql = "select*from[sheet1$]";
                OleDbCommand Cmd = new OleDbCommand(strSql, con);
                OleDbDataAdapter da = new OleDbDataAdapter(Cmd);
                DataSet ds = new DataSet();
                da.Fill(ds, "shyman");
                //dataGridView1.DataSource = ds.Tables[0];

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace);

            }
        }
        private void shanchu()
        {


            excelApp = new Microsoft.Office.Interop.Excel.Application();
            excelApp.Visible = false;
            book = excelApp.Workbooks.Open(strPath, Missing.Value, false, Missing.Value, Missing.Value, Missing.Value, true, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.Worksheets[1];
            //for (int x = 0; x < s; x++)
            //{
            //    if (shuliang >= zong - 2)
            //    {
            //        shuliang = 1;
            //    }
            //    rang = (Excel.Range)sheet.Rows[shuliang + 1, Missing.Value];
            //    rang.EntireRow.Delete(Excel.XlDeleteShiftDirection.xlShiftUp);
            //}
            book.Save();
            book.Close(Missing.Value, Missing.Value, Missing.Value);
            excelApp.Workbooks.Close();
            excelApp.Quit();
        }

        public static void ExportExcel(string str, int chan)
        {
            //string saveFileName = "";
            //bool fileSaved = false;
            //SaveFileDialog saveDialog = new SaveFileDialog();
            //saveDialog.DefaultExt = "xls";
            //saveDialog.Filter = "Excel文件|*.xls";
            //saveDialog.FileName = fileName;
            //saveDialog.ShowDialog();
            //saveFileName = saveDialog.FileName;
            //if (saveFileName.IndexOf(":") < 0) return;
            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            if (xlApp == null)
            {
                MessageBox.Show("无法创建Excel对象,可能您的电脑未安装Excel");
                return;
            }
            if (File.Exists(excelpath))
            {
                Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
                //Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
                Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Open(excelpath, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
                //Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
                Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet;


                worksheet.Cells[chan, 1] = str;




                worksheet.Columns.EntireColumn.AutoFit();//列宽自适应
                xlApp.DisplayAlerts = false;//保存Excel的时候,不弹出是否保存的窗口直接保存
                xlApp.AlertBeforeOverwriting = true;//设置禁止弹出保存和覆盖询问提示框
                try
                {
                    worksheet.SaveAs(excelpath, miss, miss, miss, miss, miss, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message + ex.StackTrace);

                }
                xlApp.Quit();
                GC.Collect();//强行销毁
            }
            else
            {
                Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
                Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
                Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];



                worksheet.Cells[chan, 1] = str;




                worksheet.Columns.EntireColumn.AutoFit();//列宽自适应
                xlApp.DisplayAlerts = false;//保存Excel的时候,不弹出是否保存的窗口直接保存
                xlApp.AlertBeforeOverwriting = true;//设置禁止弹出保存和覆盖询问提示框
                try
                {
                    worksheet.SaveAs(excelpath, miss, miss, miss, miss, miss, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message + ex.StackTrace);
                }
                xlApp.Quit();
                GC.Collect();//强行销毁
            }
        }
        public static void writeexcel(DataGridView dgv_DataQuery)
        {
            if (dgv_DataQuery.RowCount < 2)
            {
                MessageBox.Show("记录为空,不能输出到EXCEL");
                return;
            }

            SaveFileDialog fso = new SaveFileDialog();
            fso.Filter = "*.xls|*.xls";
            fso.OverwritePrompt = true;
            fso.RestoreDirectory = true;
            fso.InitialDirectory = "";

            try
            {
                if (fso.ShowDialog() == DialogResult.OK)
                {
                    FileStream fs = null;
                    fs = new FileStream(fso.FileName, FileMode.CreateNew);
                    System.IO.StreamWriter ws = new StreamWriter(fs, Encoding.Default);

                    //fs = new FileStream(fso.FileName , FileMode.CreateNew);
                    //using (StreamWriter writer = new StreamWriter(fs, Encoding.Default)) ;


                    string str = "";
                    for (int i = 0; i < dgv_DataQuery.Columns.Count; i++)
                    {
                        str += dgv_DataQuery.Columns[i].HeaderCell.Value.ToString() + "\t";
                    }
                    str += "\r\n";
                    foreach (DataGridViewRow row in dgv_DataQuery.Rows)
                    {
                        for (int i = 0; i < dgv_DataQuery.Columns.Count; i++)
                        {
                            if (!row.IsNewRow)
                            {
                                str += row.Cells[i].Value.ToString() + "\t";
                            }
                        }
                        str += "\r\n";
                    }
                    ws.Write(str);
                    ws.Dispose();
                    ws.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace);

            }
        }