编程语言
首页 > 编程语言> > C# Excel 基本操作

C# Excel 基本操作

作者:互联网

Excel开发:

 

//分别获取第一个工作表和第二个工作表
   Worksheet sheet1 = workbook.Worksheets[0];
   Worksheet sheet2 = workbook.Worksheets[1];


//基本设置

•sheet.Range["A1", "F" + (c + 2)].Borders.LineStyle = 1;  // 边框粗细

•sheet.Range["A1", "F" + (c + 2)].HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter; // 对齐方式

•sheet.Range["A1", "F1"].MergeCells = true;  // 合并单元格

•sheet.Range["A1", "F2"].Font.Bold = true;  // 加粗

•sheet.Range["A1", "F1"].Font.Size = 13;  // 字号

•sheet.Range["A1", "F" + (c + 2)].Font.Size = 11;

•sheet.Range["A1", "F" + (c + 2)].RowHeight = 21;  // 行高

•sheet.Range["A1"].RowHeight = 35;

•sheet.Range["A1", "F1"].ColumnWidth = 17;  // 列宽

•sheet.Range["B1"].ColumnWidth = 20;

•sheet.Range["A2", "F2"].Interior.Color = System.Drawing.ColorTranslator.ToOle(Color.FromArgb(197, 217, 241));  // 单元格背景颜色

•worksheet.Columns[1].ColumnWidth = 15;//设置固定值 

•worksheet.Columns.WrapText = true;//设置自动换行 

•xlApp.Columns.AutoFit();//设置自动列宽

•xlApp.Rows.AutoFit();//设置自动行高

•worksheet.Columns.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft; // 对齐方式

•worksheet.Columns[3].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft; // 对齐方式

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

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

range.Application.DisplayAlerts = false;//隐藏警告消息

range.Merge(Missing.Value);//合并单元格

 

 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);
        }




 

标签:sheet,C#,worksheet,Excel,Range,基本操作,Microsoft,Columns
来源: https://www.cnblogs.com/rtblogs/p/15502491.html