直接上代碼:
private void button1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { FormWaiting formWaiting = new FormWaiting(); formWaiting.Show(); formWaiting.Focus(); try { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "數據文件(*.xls)|*.xls|數據文件(*.xlsx)|*.xlsx"; sfd.FilterIndex = 1;//設置默認文件類型顯示順序 sfd.RestoreDirectory = true;//保存對話框是否記憶上次打開的目錄 //點了保存按鈕進入 if (sfd.ShowDialog() == DialogResult.OK) { string fullName = sfd.FileName.ToString(); //獲得文件路徑 Excel.Application xlApp = new Excel.Application(); Excel.Workbooks workbooks = xlApp.Workbooks; Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet); int count = 0; for (int i = 0; i < arrayRawInfraredImage.Length; i++) { if (arrayRawInfraredImage[i] != null) { count++; Excel.Worksheet worksheet; workbook.Worksheets.Add(System.Reflection.Missing.Value, workbook.Worksheets[count], 1, Type.Missing); worksheet = (Excel.Worksheet)workbook.Worksheets[count]; worksheet.Name = count.ToString(); bodyToSheet(worksheet, arrayRawInfraredImage[i]); } } workbook.Saved = true; workbook.SaveCopyAs(fullName); xlApp.Quit(); MessageBox.Show("已保存至" + fullName); } } catch (Exception ee) { DBConnection.Log("", ee); MessageBox.Show("保存失敗" + ee.Message); } formWaiting.Hide(); formWaiting.Dispose(); } } private void bodyToSheet(Excel.Worksheet sheet, RawInfraredImage points) { int Width = points.GetWidth(); int Height = points.GetHeight(); if (points == null) { return; } Excel.Range range = sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[Height, Width]); range.ColumnWidth = 6; range.RowHeight = 30; short[,] tempArray = points.GetRawTemperatureData(); double[,] RawTemperatureArray = new double[Height, Width]; for (int i = 0; i < Width; i++) { for (int j = 0; j < Height; j++) { if (tempArray[i, j] > points.GetMinTemperature() * 100) { RawTemperatureArray[j, Width - i - 1] = ((double)tempArray[i, j] / 100); } } } range.Value = RawTemperatureArray; }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
邏輯更清楚參考:
using Microsoft.Office.Interop.Excel;//可以使用工具選項下的nuget包管理器進行聯網下載安裝Microsoft.Office.Interop.dll using Excel = Microsoft.Office.Interop.Excel; namespace ExcelDataWD { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Button1_Click(object sender, EventArgs e) { string importExcelPath = System.Windows.Forms.Application.StartupPath + "\\imaport.xlsx"; string exportExcelPath = System.Windows.Forms.Application.StartupPath + "\\export.xlsx"; //創建 Excel.Application xlApp = new Excel.Application(); xlApp.DisplayAlerts = false; xlApp.Visible = false; xlApp.ScreenUpdating = false; //打開Excel Excel.Workbook xlsWorkBook = xlApp.Workbooks.Open(importExcelPath, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing); //處理數據過程,更多操作方法自行百度 Excel.Worksheet sheet = xlsWorkBook.Worksheets[1];//工作薄從1開始,不是0 sheet.Cells[1, 1] = "test"; //另存 xlsWorkBook.SaveAs(exportExcelPath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); //關閉Excel進程 ClosePro(xlApp, xlsWorkBook); } public void ClosePro(Excel.Application xlApp, Excel.Workbook xlsWorkBook) { if (xlsWorkBook != null) xlsWorkBook.Close(true, Type.Missing, Type.Missing); xlApp.Quit(); // 安全回收進程 System.GC.GetGeneration(xlApp); IntPtr t = new IntPtr(xlApp.Hwnd); //獲取句柄 int k = 0; GetWindowThreadProcessId(t, out k); //獲取進程唯一標志 System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k); p.Kill(); //關閉進程 } [DllImport("User32.dll", CharSet = CharSet.Auto)] public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID); } }