Jater

File_properties_write_in_Excel

Nov 14th, 2015
182
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Windows.Forms;
  3. using Excel = Microsoft.Office.Interop.Excel;
  4. using System.IO;
  5.  
  6. namespace WindowsFormsApplication1
  7. {
  8.     public partial class Form1 : Form
  9.     {
  10.     public Form1()
  11.     {
  12.         InitializeComponent();
  13.     }
  14.  
  15.     private void button1_Click(object sender, EventArgs e)
  16.     {
  17.         string[][] strFile = new string[][];
  18.         int arrRow=0;
  19.         OpenFileDialog dialog = new OpenFileDialog {
  20.             Filter = "Файлы Excel |*.xlsx"
  21.         };
  22.         if (dialog.ShowDialog() == DialogResult.OK){
  23.             Excel.Application app = new Excel.Application();
  24.             app.Workbooks.Open(dialog.FileName);
  25.             Excel.Workbook book = app.ActiveWorkbook;
  26.             Excel.Worksheet sheet = (Excel.Worksheet)book.Worksheets[1];
  27.             DirectoryInfo dir = new DirectoryInfo(@"F:\");
  28.             foreach(FileInfo files in dir.GetFiles()){
  29.                 sheet.Cells[arrRow, 1] = files.Name;
  30.                 sheet.Cells[arrRow, 2] = files.Length;
  31.                 arrRow++;
  32.             }
  33.             book.Save();
  34.             app.Quit();
  35.         }
  36.     }
  37. }
RAW Paste Data