• C# 将数据写入到Excel


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Excel = Microsoft.Office.Interop.Excel;
    using System.Reflection;

    namespace WinformWriteToExcel
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void btnWriteToExcel_Click(object sender, EventArgs e)
            {
                Excel.Application xapp = new Excel.Application(); 
                string filepath = @"D:\template.xlsx";
                Excel.Workbook xbook = xapp.Workbooks._Open(filepath, Missing.Value, Missing.Value,
                                        Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                                        Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

                Excel.Worksheet xsheet = (Excel.Worksheet)xbook.Sheets[1];

                Excel.Range rng = xsheet.get_Range("F1", Missing.Value);
                rng.Value2 = "allen(test)";


                Excel.Range rng2 = xsheet.get_Range("A4", Missing.Value);
                rng2.Value2 = "10";

                Excel.Range rng3 = xsheet.get_Range("B4", Missing.Value);
                rng3.Value2 = "20";

                Excel.Range rng4 = xsheet.get_Range("C4", Missing.Value);
                rng4.Value2 = "30";

                Excel.Range rng5 = xsheet.get_Range("D4", Missing.Value);
                rng5.Value2 = "40";

                Excel.Range rng6 = xsheet.get_Range("E4", Missing.Value);
                rng6.Value2 = "50";

                Excel.Range rng7 = xsheet.get_Range("F4", Missing.Value);
                rng7.Value2 = "60";

                Excel.Range rng8 = xsheet.get_Range("G4", Missing.Value);
                rng8.Value2 = "70";

                Excel.Range rng9 = xsheet.get_Range("H4", Missing.Value);
                rng9.Value2 = "80";

                Excel.Range rng10 = xsheet.get_Range("I4", Missing.Value);
                rng10.Value2 = "90";

                Excel.Range rng11 = xsheet.get_Range("A5", Missing.Value);
                rng11.Value2 = DateTime.Now.ToLocalTime().ToString("yyyy-MM--dd HH:mm");

                xbook.SaveAs(@"C:\test.xlsx", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                    Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

                MessageBox.Show("Write Success");
            }
        }
    }

    作者:Allen Chen无影
    邮箱:allen0717@163.com
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
  • 相关阅读:
    MSBI
    Jsoncpp 使用方法大全
    Jsoncpp的使用
    istringstream、ostringstream、stringstream 类介绍 和 stringstream类 clear函数的真正用途
    使用 GDB 调试多进程程序
    操作系统标识宏
    gdb调试多线程程序总结
    boost 1.56.0 编译及使用
    c++ bind1st 和 bind2nd的用法
    纯真IP数据库(qqwry.dat)转换成最新的IP数据库格式(ipwry.dat)
  • 原文地址:https://www.cnblogs.com/allen0118/p/1936876.html
  • Copyright © 2020-2023  润新知