New file |
| | |
| | | using Bro.M071.DBManager; |
| | | using Bro.M071.Model.Model; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.ComponentModel; |
| | | using System.Data; |
| | | using System.Drawing; |
| | | using System.IO; |
| | | using System.Linq; |
| | | using System.Text; |
| | | using System.Threading.Tasks; |
| | | using System.Windows.Forms; |
| | | |
| | | namespace ExcelTest |
| | | { |
| | | public partial class Form1 : Form |
| | | { |
| | | public Form1() |
| | | { |
| | | InitializeComponent(); |
| | | } |
| | | |
| | | private void button1_Click(object sender, EventArgs e) |
| | | { |
| | | SaveExcel(); |
| | | //SaveTxt(); |
| | | } |
| | | |
| | | public async void SaveExcel() |
| | | { |
| | | //if (!Config.IsCSVOutputEnabled) |
| | | // return; |
| | | |
| | | await Task.Run(() => |
| | | { |
| | | var measurementUnitResultAndKeyUnitDataSet = new ProductionMeasurementUnitResultAndKeyUnitDataSet(); |
| | | measurementUnitResultAndKeyUnitDataSet.KeyUnitDataList = new List<KeyUnitData>(); |
| | | measurementUnitResultAndKeyUnitDataSet.MeasurementUnitResultList = new List<MeasurementUnitResult>(); |
| | | for (int i = 0; i < 10; i++) |
| | | { |
| | | KeyUnitData keyUnitData = new KeyUnitData(); |
| | | keyUnitData.Key = "A" + i; |
| | | keyUnitData.MeasurementItem = "Z" + i; |
| | | keyUnitData.ItemValue = (i / 10).ToString(); |
| | | measurementUnitResultAndKeyUnitDataSet.KeyUnitDataList.Add(keyUnitData); |
| | | |
| | | |
| | | MeasurementUnitResult measurementUnitResult = new MeasurementUnitResult(); |
| | | measurementUnitResult.ProductionMeasurementRecordsId = Guid.NewGuid().ToString(); |
| | | measurementUnitResult.MeasurementName = "测试" + i; |
| | | measurementUnitResult.MeasurementType = "Slant"; |
| | | measurementUnitResult.MeasurementValue = (i + 10).ToString(); |
| | | measurementUnitResult.MeasurementResult = (i % 2) == 1? "OK":"NG"; |
| | | |
| | | measurementUnitResultAndKeyUnitDataSet.MeasurementUnitResultList.Add(measurementUnitResult); |
| | | } |
| | | |
| | | |
| | | ExcelExportSet excelExportDto = new ExcelExportSet(); |
| | | excelExportDto.Worksheets = new List<string>() { "原始数据", "检测结果" }; |
| | | var keyUnitColumns = new Dictionary<string, string>() |
| | | { |
| | | {"Key", "键"}, |
| | | {"MeasurementItem", "检测项"}, |
| | | {"ItemValue", "检测值"} |
| | | }; |
| | | var measurementUnitResultColumns = new Dictionary<string, string>() |
| | | { |
| | | {"MeasurementName", "检测名称"}, |
| | | {"MeasurementType", "检测类型"}, |
| | | {"MeasurementValue", "检测值"}, |
| | | {"MeasurementResult", "检测结果"}, |
| | | }; |
| | | excelExportDto.WorksheetColumns[excelExportDto.Worksheets[0]] = keyUnitColumns; |
| | | excelExportDto.WorksheetColumns[excelExportDto.Worksheets[1]] = measurementUnitResultColumns; |
| | | |
| | | excelExportDto.WorksheetDataTable[excelExportDto.Worksheets[0]] = ExcelExportHelper.ListToDataTable(measurementUnitResultAndKeyUnitDataSet.KeyUnitDataList, keyUnitColumns); |
| | | excelExportDto.WorksheetDataTable[excelExportDto.Worksheets[1]] = ExcelExportHelper.ListToDataTable(measurementUnitResultAndKeyUnitDataSet.MeasurementUnitResultList, measurementUnitResultColumns); ; |
| | | |
| | | |
| | | string dir = Path.Combine(@"D:\PROJECTS\M071", DateTime.Now.ToString("yyyyMMdd")); |
| | | if (!Directory.Exists(dir)) |
| | | { |
| | | Directory.CreateDirectory(dir); |
| | | } |
| | | var fileName = Path.Combine(dir, $"test_133536.xlsx"); |
| | | byte[] filecontent = ExcelExportHelper.CreateOrAppendExcel(excelExportDto, fileName); |
| | | FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write); |
| | | fs.Write(filecontent, 0, filecontent.Length); |
| | | fs.Flush(); |
| | | fs.Close(); |
| | | }); |
| | | } |
| | | public void SaveTxt() |
| | | { |
| | | FileStream fs = null; |
| | | string filePath = "D:\\file1.txt"; |
| | | //将待写的入数据从字符串转换为字节数组 |
| | | Encoding encoder = Encoding.UTF8; |
| | | byte[] bytes = encoder.GetBytes("Hello World! \n\r"); |
| | | try |
| | | { |
| | | //fs = File.OpenWrite(filePath); |
| | | ////设定书写的开始位置为文件的末尾 |
| | | //fs.Position = fs.Length; |
| | | |
| | | fs = File.Open(filePath, FileMode.Append, FileAccess.Write); |
| | | //将待写入内容追加到文件末尾 |
| | | fs.Write(bytes, 0, bytes.Length); |
| | | |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | Console.WriteLine("文件打开失败{0}", ex.ToString()); |
| | | } |
| | | finally |
| | | { |
| | | fs.Close(); |
| | | } |
| | | } |
| | | } |
| | | } |