向EXCEL里面写数据还有配置EXCEL单元格属性

来源:互联网 发布:网络医院 编辑:程序博客网 时间:2024/05/22 06:34

留着以后能有用

Code:
  1. using System.Reflection;   
  2. using MSExcel = Microsoft.Office.Interop.Excel;   
  3. using Microsoft.Office.Interop.Excel;   
  4.   
  5. namespace ConsoleApplication2   
  6. {   
  7.     class shelf   
  8.     {   
  9.         private static int sum = 0;   
  10.         private static MSExcel.Application appExcel;   
  11.         private static MSExcel.Workbook excelDoc;   
  12.         private static object format = MSExcel.XlFileFormat.xlWorkbookNormal;   
  13.         private static Object Nothing = Missing.Value;   
  14.         static void Main(string[] args)   
  15.         {   
  16.             int line;   
  17.             Console.Write("输入排数:");   
  18.             line = Convert.ToInt32(Console.ReadLine());   
  19.             int ShelfNumber, LayerNumber;   
  20.             Console.Write("输入货架数:");   
  21.             ShelfNumber = Convert.ToInt32(Console.ReadLine());   
  22.             Console.Write("输入层数:");   
  23.             LayerNumber = Convert.ToInt32(Console.ReadLine());   
  24.             int[] Number = new int[ShelfNumber];   
  25.             for (int i = 0; i < ShelfNumber; i++)   
  26.             {   
  27.                 Console.WriteLine("第{0}个货架列数:", i + 1);   
  28.                 Number[i] = Convert.ToInt32(Console.ReadLine());   
  29.             }   
  30.             int n;   
  31.             for (int i = LayerNumber; i > 0; i--)   
  32.             {   
  33.                 for (int j = 0; j < ShelfNumber; j++)   
  34.                 {   
  35.                     for (int k = 0; k < Number[j]; k++)   
  36.                     {   
  37.                         sum++;   
  38.                     }   
  39.                 }   
  40.             }   
  41.             string[] strings = new string[sum];   
  42.             int index=0;   
  43.             for (int i = LayerNumber; i > 0; i--)   
  44.             {   
  45.                 n = 1;   
  46.                 for (int j = 0; j < ShelfNumber; j++)   
  47.                 {   
  48.                     for (int k = 0; k < Number[j]; k++)   
  49.                     {   
  50.                         strings[index++] = line.ToString().PadLeft(2, '0') + "-" + i + "-" + n.ToString().PadLeft(2, '0');   
  51.                         n++;   
  52.                     }   
  53.                 }   
  54.             }   
  55.             shelf shelfs = new shelf();   
  56.             appExcel = new MSExcel.Application();   
  57.             excelDoc = appExcel.Workbooks.Add("D://ss.xls");   
  58.             MSExcel.Worksheet ws = (MSExcel.Worksheet)excelDoc.Sheets[1];   
  59.             string s;   
  60.             int a = 97;   
  61.             int l = 0;   
  62.             while (sum > 0)   
  63.             {   
  64.                 for (int i = 1; i <= 5 && sum > 0; i++)   
  65.                 {   
  66.                     char c = Convert.ToChar(a);   
  67.                     s = Convert.ToString(c) + i;   
  68.                     Console.WriteLine(s);   
  69.                     MSExcel.Range r = ws.get_Range(s, s);   
  70.                     //向单元格赋值   
  71.                     string sss=strings[l++];   
  72.                     //r.Value2 = "="+"/""+sss+"/"";   
  73.                     r.Value2 = sss ;   
  74.                     //r.Columns.AutoFit();   
  75.                     //r.Rows.AutoFit();   
  76.                     //r.Font.Name = "Arial Unicode MS";   
  77.                     //r.HorizontalAlignment = XlHAlign.xlHAlignCenter;   
  78.                     //r.VerticalAlignment = XlVAlign.xlVAlignCenter;   
  79.                     //r.Font.Size = 75;   
  80.                     //r.ColumnWidth = 46.75;   
  81.                     //r.RowHeight = 156.75;   
  82.                     //r.NumberFormatLocal = "@";   
  83.                     //r.Merge(true);   
  84.                     sum--;   
  85.                 }   
  86.                 a++;   
  87.             }   
  88.             excelDoc.SaveAs("D://ss.xls", Nothing, Nothing, Nothing, Nothing, Nothing,   
  89.                 MSExcel.XlSaveAsAccessMode.xlExclusive, Nothing, Nothing, Nothing, Nothing, Nothing);   
  90.             excelDoc.Close(Nothing, Nothing, Nothing);   
  91.             appExcel.Quit();   
  92.         }   
  93.     }   
  94. }