c# 导出数据到excel

直接上代码:

        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);
    }
}
原文地址:https://www.cnblogs.com/gaara-zhang/p/10382209.html