C#关于excel文件自动生成的写法
2009-08-21 18:00
344 查看
作为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);
}
}
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);
}
}
相关文章推荐
- yii phpexcel自动生成文件保存到服务器上
- 使用POI生成Excel文件,可以自动调整excel列宽
- JAXB注解 java 关于xml的注解,自动生成xml文件
- 使用POI生成excel文件,可以自动调整列宽等
- 关于corba接口自动生成把idl文件编译成Java的批处理文件(win bat)的java方法(原创)
- 初学Phreeze 5 --excel 表格,自动生成文件
- 用groovy简化java反射编写一个基于行数据对象的自动生成excel文件对象的工具
- 使用POI生成Excel文件,可以自动调整excel列宽
- 关于Android Studio AIDL 无法自动生成Java文件
- 关于automake等工具自动生成Makefile文件的一些技术摘要
- 关于ashx文件的.cs自动生成
- 关于Matlab中自动生成的asv文件
- Perl Spreadsheet::WriteExcel 模块自动生成excel 文件
- 网站的robots 文件写法 附自动生成工具网址 推荐
- 使用POI生成Excel文件,可以自动调整excel列宽等
- 使用POI生成Excel文件,可以自动调整excel列宽
- 关于.Pro文件中引入源文件的不同写法,导致QMake无法生成正确的Makefile文件的错误
- 使用POI生成Excel文件,可以自动调整excel列宽 .
- JAXB注解 java 关于xml的注解,自动生成xml文件
- 关于不调用word excel 等等的com对象生成doc文件或者xls文件