C# 操作excel单元格居中

Posted 苏州城外的微笑

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# 操作excel单元格居中相关的知识,希望对你有一定的参考价值。

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;//边框常规粗细  

 

以上是关于C# 操作excel单元格居中的主要内容,如果未能解决你的问题,请参考以下文章

Python 技术篇 - 操作excel实现单元格合并并居中实例演示,用openpyxl库为指定区域的单元格设置对齐样式和字体样式方法

在EXCEL中把上下两单元格合并了,怎样才能使文字上下居中呢?

如何让datagridview单元格只能输入数字 C#

phpexcel 合并单元格后插入图片 如何让图片居中

有用aspose.words的吗,请教单元格内容怎么水平居中

excel怎么居中单元格