| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;//正则表达式所在空间
- using System.Data;
- using System.Windows.Forms;
- using Microsoft.Office.Interop.Excel;
- using Infragistics.Win;
- namespace Core.LZMes.Client.UIB
- {
- class JhyOpe
- {
- //double类型格式判断
- public static bool DcsDouble(string str)
- {
- Boolean bl = true;//默认为真,是字符串格式
- Regex r = new Regex(@"^\d+(\.\d+)?\d?$");
- if (str == "合格" || str == "不合格")//合格与不合格作为满足来处理
- {
- bl = true;
- }
- else
- {
- Match m = r.Match(str);
- if (!m.Success)
- {
- bl = false;
- }
- }
- return bl;
- }
- /// <summary>
- /// </summary>
- /// <param name="dt"></param>
- /// <param name="path">导出路径</param>
- /// <param name="fileName">文件名</param>
- /// <param name="modelExcel">模板名</param>
- public void Export(System.Data.DataTable dt, string path , string fileName , string modelExcel)
- {
- try
- {
- if (path == ""){MessageBox.Show("请选择导出路径" , "提示");return;}
- ApplicationClass ExcelApp = null;
- Microsoft.Office.Interop.Excel.Application excel = null;
- Microsoft.Office.Interop.Excel.Workbooks workbooks = null;
- Microsoft.Office.Interop.Excel._Workbook workbook = null;
- Microsoft.Office.Interop.Excel.Sheets sheets = null;
- Microsoft.Office.Interop.Excel.Worksheet sheet = null;
- //导出
- string strfile = System.Windows.Forms.Application.StartupPath + "\\" + modelExcel;//模板路径
- ExcelApp = new ApplicationClass();
- excel = new Microsoft.Office.Interop.Excel.Application();
- workbooks = excel.Workbooks;
- workbook = workbooks.Add(strfile);
- sheets = workbook.Sheets;
- sheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets[1];
- if (fileName == "热轧拉力试验原始记录表")
- setData_1(dt , sheet);
- else if (fileName == "热轧冲击试验原始记录表")
- setData_2(dt , sheet);
-
- workbook.Saved = true;
- workbook.SaveCopyAs(path + "\\" + fileName + ".xls");//fileName如:热轧拉力试验原始记录表
- excel.DisplayAlerts = false;
- excel.AlertBeforeOverwriting = false;
- workbooks.Close();
- workbook = null;
- excel.Quit();
- excel = null;
- ExcelApp.Quit();
- ExcelApp = null;
- }
- catch (Exception ex)
- {
- System.Diagnostics.Debug.WriteLine(ex.ToString());
- }
- }
- //热轧拉力试验原始记录表
- private void setData_1(System.Data.DataTable dt , Microsoft.Office.Interop.Excel.Worksheet sheet)
- {
- int startRow = 5;//模板数据填充从第五行开始
- Range range = null;
- int i = 0;
- int j = 1;
- for (i = 0; i < dt.Rows.Count; i++)
- {
- j = 1;
- sheet.Cells[startRow + i, j++] = dt.Rows[i]["SMP_STEEL_NO"].ToString();//钢印号
- sheet.Cells[startRow + i, j++] = dt.Rows[i]["SPEC_STL_GRD"].ToString();//牌号
- sheet.Cells[startRow + i, j++] = dt.Rows[i]["MATERIAL_NO"].ToString();//钢卷
- sheet.Cells[startRow + i, j++] = dt.Rows[i]["MATERIAL_THK"].ToString();//规格
- sheet.Cells[startRow + i, j++] = dt.Rows[i]["LA"].ToString();//a
- sheet.Cells[startRow + i, j++] = dt.Rows[i]["LB"].ToString();//b
- j++;
- j++;
- j++;
- j++;
- sheet.Cells[startRow + i, j++] = dt.Rows[i]["FE"].ToString();//Fe
- sheet.Cells[startRow + i, j++] = dt.Rows[i]["FM"].ToString();//Fm
- sheet.Cells[startRow + i, j++] = dt.Rows[i]["LO"].ToString();//Lo
- sheet.Cells[startRow + i, j++] = dt.Rows[i]["LU"].ToString();//Lu
- // sheet.Cells[startRow + i, j++] = dt.Rows[i]["ZJ"].ToString();//直径
- // sheet.Cells[startRow + i, j++] = dt.Rows[i]["CB"].ToString();//冷弯
- }
- range = sheet.get_Range(sheet.Cells[startRow, 1], sheet.Cells[startRow + (i - 1), j - 1 + 3]);//(Range)sheet.Rows[6, 1];
- range.Borders.LineStyle = 1;
- range.NumberFormatLocal = "@";//设置单元格格式为文本
- range.RowHeight = 22.5;
- //range.HorizontalAlignment = XlHAlign.xlHAlignLeft;//字体左靠
- // range.RowHeight = 24;
- // System.DateTime dt = System.DateTime.Now;
- // string TimeNow = string.Format("{0:yyyyMMddHHmmssffff}", dt);
- }
- //冲击试验原始记录表
- private void setData_2(System.Data.DataTable dt, Microsoft.Office.Interop.Excel.Worksheet sheet)
- {
- int startRow = 5;//模板数据填充从第五行开始
- Range range = null;
- int i = 0;
- int j = 1;
- for (i = 0; i < dt.Rows.Count; i++)
- {
- j = 1;
- sheet.Cells[startRow + i, j++] = dt.Rows[i]["SMP_STEEL_NO"].ToString();//钢印号
- sheet.Cells[startRow + i, j++] = dt.Rows[i]["SPEC_STL_GRD"].ToString();//牌号
- sheet.Cells[startRow + i, j++] = dt.Rows[i]["MATERIAL_NO"].ToString();//钢卷
- sheet.Cells[startRow + i, j++] = dt.Rows[i]["MATERIAL_THK"].ToString();//规格
- j++;//温度
- sheet.Cells[startRow + i, j++] = dt.Rows[i]["A1"].ToString();//A1
- sheet.Cells[startRow + i, j++] = dt.Rows[i]["A2"].ToString();//A2
- sheet.Cells[startRow + i, j++] = dt.Rows[i]["A3"].ToString();//A3
- //sheet.Cells[startRow + i, j++] = dt.Rows[i]["FM"].ToString();//Fm
- //sheet.Cells[startRow + i, j++] = dt.Rows[i]["LO"].ToString();//Lo
- //sheet.Cells[startRow + i, j++] = dt.Rows[i]["LU"].ToString();//Lu
- //sheet.Cells[startRow + i, j++] = dt.Rows[i]["ZJ"].ToString();//直径
- //sheet.Cells[startRow + i, j++] = dt.Rows[i]["CB"].ToString();//冷弯
- }
- //设置样式
- range = sheet.get_Range(sheet.Cells[startRow, 1], sheet.Cells[startRow + (i - 1), j - 1 + 1]);//(Range)sheet.Rows[6, 1];
- range.Borders.LineStyle = 1;
- range.NumberFormatLocal = "@";//设置单元格格式为文本
- range.RowHeight = 22.5;
- }
- public static void ttt()
- {
-
- //CheckBoxUIElement checkBoxUIElement = parent.GetDescendant(typeof(CheckBoxUIElement)) as CheckBoxUIElement;
- }
- }
- }
|