C#关于excel文件自动生成的写法

作为C#学习的开始,先做一个EXCEL的自动生成工具,比较简陋不过总比什么都不知道是有进步的。

 

        public void Optxt(String sPath)
        {
            sPathTemp = sPath;

            // フォルダ (ディレクトリ) が存在しているかどうか確認する
            if (System.IO.Directory.Exists(sPath))
            {
                // 必要な変数を宣言する
                string stPrompt = string.Empty;

                // 拡張子が .txt のファイルを列挙する
                foreach (string stFilePath in System.IO.Directory.GetFiles(sPath, "*.txt"))
                {
                    stPrompt += stFilePath + System.Environment.NewLine;
                }

 

System.Environment真是很强大,只要WINDOWS存在的都可以使用的感觉。


                // 取得したすべてのファイルパスを表示する
                if (stPrompt == "")
                {
                    MessageBox.Show("TXTファイルがありません");
                }
                else
                {
                    txtList = System.IO.Directory.GetFiles(sPath, "*.txt");
                    // Excelファイルに展開
                    sheetCreate( );
                }
            }
            else
            {
                MessageBox.Show("ディレクトリは存在しません");
            }
        }


        // Excelファイルに展開を実行
        private void sheetCreate( )
        {
            Excel.Application oXL;
            Excel._Workbook oWB;
            Excel._Worksheet oSheet;
Excel害苦了我呀,OFFICE2003之前是安装过的,但是并不代表相应的命名空间就是存在的,在没有安装

OFFICE的.net支持控件之前使用不了的,要注意!!

还有就是Excel._Workbook和Excel.Workbook的区别了,他们并不是一样的东东,这个还要在研究一下


            int i = 1;
            bool bTemp = false;
            string sFileName = string.Empty;
            ArrayList aList = new ArrayList();
           
            oXL = new Excel.Application();
            oWB = (Excel._Workbook)(oXL.Workbooks.Add( Excel.XlWBATemplate.xlWBATWorksheet ));

BOOK追加的同时一个工作表就被追加上了SHEET1

 

              oWB.Sheets.Add(System.Reflection.Missing.Value,
                oWB.Sheets[oWB.Sheets.Count],
                sSheet.Length - 1,
                System.Reflection.Missing.Value);


            foreach (string sTempSheet in sSheet)
            {
                // TXTファイルが存在するか確認
                foreach (string sTemp in txtList)
                {
                    if (sTemp.IndexOf(sTempSheet, 1)> 0)
                    {
                        bTemp = true;
                        break;
                    }
                }
                // 存在しないの場合は、次へ
                if (!bTemp)
                {
                    MessageBox.Show("存在しないTXTファイルが発見:" + sTempSheet);
                    Environment.Exit(0);
                }
                bTemp = false;
            }

            foreach (string sTempSheet in sSheet)
            {
                // StreamReader の新しいインスタンスを生成する
                System.IO.StreamReader cReader = (
                    new System.IO.StreamReader(sPathTemp + "//" + sTempSheet, System.Text.Encoding.UTF8)
                    );

                // 読み込みできる文字がなくなるまで繰り返す
                while (cReader.Peek() >= 0)
                {
                    // ファイルを 1 行ずつ読み込む
                    string stBuffer = cReader.ReadLine();
                    // 区切りで項目内容を取得
                    string[] sComm = stBuffer.Split(',');

                    aList.Add(sComm);
                }

                // cReader を閉じる (正しくは オブジェクトの破棄を保証する を参照)
                cReader.Close();
               
                try
                {
                    oSheet = (Excel._Worksheet)oWB.Sheets.get_Item("sheet" + i);


                    oSheet.Name = sTempSheet;
                    i++;
                    // ワークシートの名前付き範囲に値を送信する                   
                    for (int iSize = 0; iSize < aList.Count; iSize++)
                    {   
                        string[] sT = (string[])aList[iSize];
                        for(int sSize = 0; sSize < sT.Length; sSize++)
                        {
                            oSheet.Cells[iSize + 1, sSize + 1] = sT[sSize].Replace("/"","");
                        }
                    }
                }
                catch (System.Exception e)
                {
                  throw e;
                }
                finally
                {
                       
                }
                bTemp = false;
                aList.Clear();
            }
           
            // ファイルを保存
            oWB.SaveAs(
                sPathTemp+"//export.xls",
                System.Reflection.Missing.Value,
                System.Reflection.Missing.Value,
                System.Reflection.Missing.Value,
                System.Reflection.Missing.Value,
                System.Reflection.Missing.Value,
                Excel.XlSaveAsAccessMode.xlExclusive,
                System.Reflection.Missing.Value,
                System.Reflection.Missing.Value,
                System.Reflection.Missing.Value,
                System.Reflection.Missing.Value,
                System.Reflection.Missing.Value
                );

SaveAs的所谓同类还有几个,保存的几个方法,以后有时间可以看看。


            MessageBox.Show("Excelファイル作成が完了!");

          // ワークブック終了
          try
          {
            oWB.Close(System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
          }
          finally
          {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oWB);
          }
          // エクセル終了
          try
          {
            oXL.Quit();
          }
          finally
          {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oXL);
          }
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值