-2
私はC#のデータテーブルからExcelをエクスポートするプログラムを作成しました。既定では、セルの形式は一般に固定されています。どのようにプログラムを介してセルの形式を変更するには? (フォーマット例:テキスト、番号など)。ここで は私のコードCからエクスポート中にExcelセルをフォーマットする方法
private void WriteExcel(DataTable datatable, string FileName)
{
StringBuilder SBuild = new StringBuilder();
using (StreamWriter sw2Excel = File.CreateText(FileName))
{
//get the Columns of the DataTable in the 1st Line
for (int i = 0; i < datatable.Columns.Count; i++)
{
SBuild.Append(datatable.Columns[i].ColumnName + "\t");
}
sw2Excel.WriteLine(SBuild.ToString());
//Fill The Rows To the Excel File
for (int i = 0; i < datatable.Rows.Count; i++)
{
StringBuilder swRowSb = new StringBuilder();
for (int k = 0; k < datatable.Columns.Count; k++)
{
swRowSb.Append(datatable.Rows[i][k].ToString().Replace(',', ' ') + "\t");
}
sw2Excel.WriteLine(swRowSb.ToString());
}
Microsoft.Office.Interop.Excel.ApplicationClass app = new Microsoft.Office.Interop.Excel.ApplicationClass();
Microsoft.Office.Interop.Excel.Workbook workBook = app.Workbooks.Open(FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, ",", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
workBook.SaveAs(FileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing
, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
app.Workbooks.Close();
app.Quit();
}
}