C# worksheet设置Excel样式

C# worksheet设置Excel样式,第1张

C# worksheet设置Excel样式

1.例子导出Excel的样式

样式代码

  public void Exportdatagridviewtoexcel(string Textname)
{ SaveFileDialog savedialog = new SaveFileDialog();
savedialog.DefaultExt = "xlsx";
savedialog.Filter = "microsoft office execl files (*.xlsx)|*.xlsx";
savedialog.FilterIndex = ;
savedialog.RestoreDirectory = true;
savedialog.Title = "导出数据到excel表格";
savedialog.ShowDialog();
if (savedialog.FileName.IndexOf(":") < ) return; //被点了取消
//Microsoft.office.interop.excel.application xlapp = new microsoft.office.interop.excel.application();
Microsoft.Office.Interop.Excel.Application xlapp = new Microsoft.Office.Interop.Excel.Application();
if (xlapp == null)
{
MessageBox.Show("可能您的机子未安装excel,无法创建excel对象!", "系统提示 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
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[];//取得sheet1 string ADD = "盘点录入明细表 时间:" + DateTime.Now.ToShortDateString(); worksheet.get_Range(worksheet.Cells[, ], worksheet.Cells[, ]).MergeCells = true;
//worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, 7]).Font.Bold = true;//
worksheet.Cells[, ] = " " + Textname;
worksheet.Cells[, ] = "" + "使用科室";
worksheet.get_Range(worksheet.Cells[, ], worksheet.Cells[, ]).MergeCells = true;
worksheet.get_Range(worksheet.Cells[, ], worksheet.Cells[, ]).MergeCells = true;
//worksheet.Cells[2, 5] = "" + "年月日";
worksheet.Cells[, ] = "患者姓名";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = "性别";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = "年龄";
worksheet.Cells[, ] = "住院号";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = "床位号";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = "手术名称";
worksheet.Cells[, ] = "使用部位";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = "手术日期";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = "医保类型"; worksheet.get_Range(worksheet.Cells[, ], worksheet.Cells[, ]).MergeCells = true;
worksheet.get_Range(worksheet.Cells[, ], worksheet.Cells[, ]).MergeCells = true;
worksheet.get_Range(worksheet.Cells[, ], worksheet.Cells[, ]).MergeCells = true;
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = "生产厂家";
worksheet.get_Range(worksheet.Cells[, ], worksheet.Cells[, ]).MergeCells = true;
worksheet.Cells[, ] = "注册证号";
worksheet.get_Range(worksheet.Cells[, ], worksheet.Cells[, ]).MergeCells = true;
worksheet.Cells[, ] = "生产日期";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = "灭菌日期";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = "有效日期";
worksheet.get_Range(worksheet.Cells[, ], worksheet.Cells[, ]).MergeCells = true;
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = "供货单位";
worksheet.get_Range(worksheet.Cells[, ], worksheet.Cells[, ]).MergeCells = true;
worksheet.Cells[, ] = "供货商签字";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = "联系电话";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = "产品名称";
worksheet.Cells[, ] = "规格型号";
worksheet.Cells[, ] = "产品批号";
worksheet.Cells[, ] = "单价";
worksheet.Cells[, ] = "数量";
worksheet.Cells[, ] = "单位";
worksheet.Cells[, ] = "金额 "; worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = " "; worksheet.Cells[, ] = "高值耗材使用医生签字";
worksheet.Cells[, ] = "手术护士";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = " ";
worksheet.get_Range(worksheet.Cells[, ], worksheet.Cells[, ]).MergeCells = true;
worksheet.get_Range(worksheet.Cells[, ], worksheet.Cells[, ]).MergeCells = true;
worksheet.Cells[, ] = "收费人员(记账人员)签字";
worksheet.Cells[, ] = "收费价格";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = " ";
worksheet.get_Range(worksheet.Cells[, ], worksheet.Cells[, ]).MergeCells = true;
worksheet.get_Range(worksheet.Cells[, ], worksheet.Cells[, ]).MergeCells = true;
worksheet.Cells[, ] = "科室负责人签字";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = " ";
worksheet.Cells[, ] = "设备采购签字";
worksheet.Cells[, ] = " ";
//worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[2, 10]).Text = ADD;
worksheet.get_Range(worksheet.Cells[, ], worksheet.Cells[, ]).MergeCells = true;
worksheet.get_Range(worksheet.Cells[, ], worksheet.Cells[, ]).MergeCells = true; //worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[2, 8]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;
//worksheet.get_Range(worksheet.Cells[3, 1], worksheet.Cells[3, 8]).Font.Bold = true;
//worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[2, 8]).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThick;
//worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[2, 8]).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThick;
//worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[2, 8]).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThick;
//worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[2, 8]).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThick;
worksheet.Cells[, ] = "高值耗材唯一性标示";
//worksheet.get_Range(worksheet.Cells[17, 1], worksheet.Cells[17, 1]).VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;
worksheet.get_Range(worksheet.Cells[, ], worksheet.Cells[, ]).MergeCells = true;
worksheet.get_Range(worksheet.Cells[, ], worksheet.Cells[, ]).MergeCells = true;
worksheet.get_Range(worksheet.Cells[, ], worksheet.Cells[, ]).WrapText = true;//
worksheet.get_Range(worksheet.Cells[, ], worksheet.Cells[, ]).Borders.Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin;
worksheet.get_Range(worksheet.Cells[, ], worksheet.Cells[, ]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; if (savedialog.FileName != "")
{
try
{
workbook.Saved = true;
workbook.SaveCopyAs(savedialog.FileName);
}
catch (Exception ex)
{
MessageBox.Show("导出文件时出错,文件可能正被打开!..." + ex.Message, "系统提示 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
//GC.Collect();//强行销毁
MessageBox.Show("数据导出成功! ", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); }

2.单元格样式设置

Excel.Range r =mySheet.get_Range(mySheet.Cells[,],mySheet.Cells[DT.Rows.Count + ,DT.Columns.Count - ]);

r.NumberFormat="@";  //设置单元格格式为文本类型,文本类型可设置上下标

r.NumberForma="0.00_ "//设置单元格格式为数值类型,小数点后2位

r.NumberForma="¥#,##0.00;¥-#,##0.00"//设设置单元格格式为货币类型,小数点后2位

r.NumberForma=_"_ ¥*#,##0.00_;_ ¥*-#,##0.00_ ;_ ¥*""-""??_;_ @_ "//置单元格格式为会计专用类型,小数点后2位

r.NumberForma="yyyy-m-d"//设置单元格格式为日期类型

r.NumberForma="[$-F400]h:mm:ss AM/PM"//设置单元格格式为时间类型

r.NumberForma="0.00%"//设置单元格格式为百分比类型,小数点后2位

r.NumberForma="# ?/?"设置单元格格式为分数类型,分母为一位数

r.NumberForma="0.00E+00"//设置单元格格式为科学技术类型,小数位数为2

r.NumberForma=""//设置单元格格式为特殊类型

B、上下标格式的控制;

mySheet.get_Range(mySheet.Cells[i+, DT.Columns.Count - ],mySheet.Cells[i + , DT.Columns.Count -]).get_Characters(a.Length+ ,b.Length).Font.Subscript =true;//控制输出样式为下标

mySheet.get_Range(mySheet.Cells[i+, DT.Columns.Count - ],mySheet.Cells[i + , DT.Columns.Count-]).get_Characters(a.Length + b.Length+ , c.Length).Font.Superscript =true; //控制输出样式为上标

C、单元格水平垂直对齐方式;

//单元格水平,垂直居中

r.HorizontalAlignment=Excel.XlHAlign.xlHAlignCenter;

r.VerticalAlignment=Excel.XlVAlign.xlVAlignCenter;

上面代码中,枚举XLHAlign的值还有:

// 右对齐

xlHAlignRight,

// 左对齐.

xlHAlignLeft,

// 两端对齐.

xlHAlignJustify,

// 分散对齐(缩进)

xlHAlignDistributed,

// 居中对齐

xlHAlignCenter,

// 依照数据类型对齐,常规

xlHAlignGeneral,

// 填充

xlHAlignFill,

// 跨列对齐.

xlHAlignCenterAcrossSelection=,

枚举XLVAlign的值还有:

// 靠上对齐

xlVAlignTop,

//两端对齐.

xlVAlignJustify=-,

//分散对齐.

xlVAlignDistributed,

//居中对齐.

xlVAlignCenter,

//靠下对齐.

xlVAlignBottom=-,

D、 单元格边框设置;

//设置边框

Excel.Range r =mySheet.get_Range(mySheet.Cells[,],mySheet.Cells[DT.Rows.Count + ,DT.Columns.Count - ]);

r.Borders.LineStyle=Excel.XlLineStyle.xlContinuous;

枚举XlLineStyle中还有下面线形:

//没边框线

xlLineStyleNone,

//双线.

xlDouble,

//点状线.

xlDot,

//虚线.

xlDash,

//连续线.

xlContinuous,

//点线交互型

xlDashDot,

//两点一线型

xlDashDotDot,

//斜线.

xlSlantDashDot,

E、单元格合并 

用get_Range方法获取要合并的单元格,再设置MergeCells属性的值进行合并。


代码: //合并单元格 myExcel.get_Range(myExcel.Cells[,],myExcel.Cells[,]).MergeCells =true; F、字体设置 先用get_Range方法选中要设置字体的某个单元格或者或者直接用get_Characters方法直接选中要设置的字符进行设置; 代码: //加粗字体 myExcel.get_Range(myExcel.Cells[,],myExcel.Cells[,]).Font.Bold =true; //设置字体大小 myExcel.get_Range(myExcel.Cells[,],myExcel.Cells[,]).Font.Size=; //设置字体的颜色 myExcel.get_Range(myExcel.Cells[,],myExcel.Cells[,]).Font.ColorIndex = ; //设置字体 myExcel.get_Range(myExcel.Cells[,],myExcel.Cells[,]).Font.Name ="隶书"; //设置成斜体 myExcel.get_Range(myExcel.Cells[,],myExcel.Cells[,]).Font.Italic =true; //设置下划线 myExcel.get_Range(myExcel.Cells[,],myExcel.Cells[,]).Font.Underline =true; G、冻结行列 用get_Range方法获取单元格,再设置其Freezepanes属性为true,就把该单元格右上方的行和列都冻结了,取消冻结将其值设为false即可。


//冻结行列 myExcel.get_Range(myExcel.Cells[,],myExcel.Cells[,]).Activate(); myExcel.ActiveWindow.FreezePanes=true; H、 公式输入栏的隐藏 //设置是否显示Excel公式输入栏,默认为true myExcel.DisplayFormulaBar=false; I、 列标题与行标题的隐藏 //设置是否显示行和列的标题,默认为true myExcel.ActiveWindow.DisplayHeadings=false; J、网格的隐藏 //设置是否显示网格,默认为true myExcel.ActiveWindow.DisplayGridlines=false; K、 水平、垂直滚动条的隐藏 //设置是否显示水平滚动条 myExcel.ActiveWindow.DisplayHorizontalScrollBar=false; //设置是否显示垂直滚动条 myExcel.ActiveWindow.DisplayVerticalScrollBar=false; L、页的隐藏Sheet //设置是否显示Sheet页 myExcel.ActiveWindow.DisplayWorkbookTabs=false; M、 电子表格外观控制: Sheet.DisplayToolbar= false;//是否显示工具栏默认值为 true Sheet.DisplayOfficeLogo= false;// 取消显示Office图标 Sheet.DisplayColumnHeadings= false;// 是否显示列标题,默认是true Sheet.DisplayRowHeadings= false;// 是否显示行标题,默认是true Sheet.DisplayHorizontalScrollBar= false;// 是否显示水平滚动条,默认为true Sheet.DisplayVerticalScrollBar= false;// 是否显示垂直滚动条,默认为true Sheet.DisplayGridlines= false;// 电子表格是否显示网格,默认为true for (int i = ; i < DT.Columns.Count- ; i += ) { Sheet.get_Range(Sheet.Rows.Cells[i,],Sheet.Rows.Cells[i, DT.Columns.Count - ]).Interior.set_ColorIndex(); //设置行的颜色 } worksheet.Cells[, ]= "Excel单元格赋值";//Excel单元格赋值
range.Font.Size = ; //设置字体大小
range.Font.Underline=true; //设置字体是否有下划线
range.Font.Name="黑体";设置字体的种类
range.HorizontalAlignment=XlHAlign.xlHAlignCenter; //设置字体在单元格内的对其方式
range.ColumnWidth=; //设置单元格的宽度
range.Cells.Interior.Color=System.Drawing.Color.FromArgb(,,).ToArgb();//设置单元格的背景色
range.Borders.LineStyle=; //设置单元格边框的粗细
range.BorderAround(XlLineStyle.xlContinuous,XlBorderWeight.xlThick,XlColorIndex.xlColorIndexAutomatic,System.Drawing.Color.Black.ToArgb());//给单元格加边框
range.Borders.get_Item(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop).LineStyle=Microsoft.Office.Interop.Excel.XlLineStyle.xlLineStyleNone;//设置单元格上边框为无边框
range.EntireColumn.AutoFit(); //自动调整列宽
Range.HorizontalAlignment= xlCenter; // 文本水平居中方式
Range.VerticalAlignment= xlCenter //文本垂直居中方式
Range.WrapText=true; //文本自动换行
Range.Interior.ColorIndex=; //填充颜色为淡紫色
Range.Font.Color=clBlue; //字体颜色
xlsApp.DisplayAlerts=false; //保存Excel的时候,不d出是否保存的窗口直接进行保存 向Excel中填充数据,并保存为临时文件 Excel.ApplicationmyExcel =newExcel.Application(); //打开模板文件 myExcel.Application.Workbooks.Open(mode.FullName,missing,missing,missing, missing, missing, missing, missing, missing,missing, missing,missing,missing, missing, missing); //选中有数据的Cells Excel.WorkbookmyBook = myExcel.Workbooks[]; Excel.WorksheetmySheet = (Excel.Worksheet)myBook.Worksheets[]; Excel.Ranger = mySheet.get_Range(mySheet.Cells[,],mySheet.Cells[DT.Rows.Count + ,DT.Columns.Count - ]); r.Select(); //不单独显示Excel,最后在IE中显示 myExcel.Visible=false; //第一行为报表的标题 myExcel.Cells[,]= "用模板导出的报表"; //逐行写入数据,数组中第一行为报表的列标题 for (int i = ; i <DT.Columns.Count - ; i++) { myExcel.Cells[,+ i] =DT.Columns[i].Caption; ; } //在当前目录下指定一个临时文件 string FileName =Server.MapPath("~")+"""Temp.xls"; if (File.Exists(FileName)) { File.Delete(FileName); } myExcel.Save(FileName); mySheet.Cells.Clear(); //设置不出现保存提示框 myBook.Saved=true; myExcel.Application.Workbooks.Close();

框架4.0之上的导出  引用 Aspose.cell

 public void importExcel(DataTable dt, string fileName, string filePath)
{
int posStart = ;//报表内容起始
//设置字体样式
Style style1 = new Style();
style1.HorizontalAlignment = TextAlignmentType.Center;//文字居中
style1.VerticalAlignment = TextAlignmentType.Left;
style1.Font.Name = "宋体";
style1.Font.IsBold = true;//设置粗体
style1.Font.Size = ;//设置字体大小 Style style2 = new Style();
style2.HorizontalAlignment = TextAlignmentType.Center;
style2.Font.Size = ; Workbook wb = new Workbook();
Worksheet ws = wb.Worksheets[];
Cells cell = ws.Cells; int colCount = dt.Columns.Count;
int rowCount = dt.Rows.Count;
//合并第一行单元格,显示标题
Range range = cell.CreateRange(, , , colCount);
range.Merge();
cell["A1"].PutValue(fileName); //标题
cell.SetRowHeight(, );//设置行高
//给单元格关联样式
cell["A1"].SetStyle(style1); //报表名字 样式
//设置Execl列名
for (int i = ; i < colCount; i++)
{
cell[, i].PutValue(dt.Columns[i].ColumnName);
//cell[1, i].SetStyle(style2);
}
//设置单元格内容
for (int i = ; i < rowCount; i++)
{
for (int j = ; j < colCount; j++)
{
cell[i + posStart, j].PutValue((dt.Rows[i][j] + "").Trim());
//cell[i + posStart, j].SetStyle(style2);
}
}
//设置列宽
//for (int i = 1; i < dt.Columns.Count; i++)
//{
// cell.SetColumnWidthPixel(i - 1, int.Parse(dataGridView1.Columns[i].Width.ToString()));
//}
//保存excel表格
wb.Save(filePath); }

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/588939.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-12
下一篇 2022-04-12

发表评论

登录后才能评论

评论列表(0条)

保存