作为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);
}
}
C# Excel自动化
本文介绍了一个用C#实现的简单Excel自动生成工具。该工具能够从指定目录下的多个TXT文件中读取数据,并将这些数据整合到一个Excel文件中,每个TXT文件对应一个Excel工作表。
1389

被折叠的 条评论
为什么被折叠?



