C# Excel 基本操作
2021/11/3 11:10:12
本文主要是介绍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);
}
这篇关于C# Excel 基本操作的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-12-06使用Microsoft.Extensions.AI在.NET中生成嵌入向量
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#