C# 操作excel

//导出Excel
        private void ExportExcel(string fileName, System.Data.DataTable myDGV, string title)
        {
            string saveFileName = "";
            System.Windows.Forms.SaveFileDialog saveDialog = new System.Windows.Forms.SaveFileDialog();
            saveDialog.DefaultExt = "xlsx";
            saveDialog.Filter = "Excel文件|*.xlsx";
            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)
            {
                System.Windows.MessageBox.Show("无法创建Excel对象,可能您的机子未安装Excel");
                return;
            }
            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];//取得sheet1

//操作单元格
            Range rangeLecture = worksheet.Range[worksheet.Cells[1, 1], worksheet.Cells[1, myDGV.Columns.Count]];
            rangeLecture.Application.DisplayAlerts = false;
            rangeLecture.Merge(Missing.Value);
            rangeLecture.Application.DisplayAlerts = true;
            worksheet.Cells[1, 1] = title;

//写入标题
            for (int i = 0; i < myDGV.Columns.Count; i++)
            {
                worksheet.Cells[2, i + 1] = myDGV.Columns[i].ColumnName;
            }
            //写入数值
            for (int r = 0; r < myDGV.Rows.Count; r++)
            {
                for (int i = 0; i < myDGV.Columns.Count; i++)
                {
                    worksheet.Cells[r + 3, i + 1] = myDGV.Rows[r][i].ToString();
                }
                System.Windows.Forms.Application.DoEvents();
            }
            worksheet.Columns.EntireColumn.AutoFit();//列宽自适应
            if (saveFileName != "")
            {
                try
                {
                    workbook.Saved = true;
                    workbook.SaveCopyAs(saveFileName);
                }
                catch (Exception ex)
                {
                    System.Windows.MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message);
                }
            }
            xlApp.Quit();
            GC.Collect();//强行销毁
            //System.Windows.Forms.MessageBox.Show("文件保存成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

操作单元格样式

worksheet.Cells[1, 1].Value2 = strTitle; //设置单元格内文本

worksheet.Cells[1, 1].Font.Name = "宋体";//设置字体

worksheet.Cells[1, 1].Font.Size = 18;//字体大小

worksheet.Cells[1, 1].Font.Bold = true;//加粗显示

worksheet.Cells[1, 1].HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;//水平居中

worksheet.Cells[1, 1].VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;//垂直居中

worksheet.Cells[1, 1].Borders.LineStyle = Excel.XlLineStyle.xlContinuous;//设置边框

worksheet.Cells[1, 1].Borders.Weight = Excel.XlBorderWeight.xlMedium;//边框常规粗细

05-11 09:41