各位高手:
以下为我汇出EXCEL的代码:汇出的很慢,还有其它更好的办法或者是优化
//初始化Excel組件
Excel.Application excel = new Excel.Application();
Excel.Workbook workbookData = excel.Application.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.Worksheet xlSheet = (Excel.Worksheet)workbookData.Worksheets[1];
//設定Excel標題
for (int i = 0; i < DataColNum; i++)
{
//獲取列名
string ColName = dt.Tables[0].Columns[i].ColumnName.ToString().Trim();
xlSheet.Cells[1, i + 1] = sys.ChineseCstr(ColName);
}
fom.GetType().GetProperty("PbValue").SetValue(fom, 2, null);
fom.Refresh();
//設定Excel資料區
for (int j = 0; j < DataRowNum; j++)
{
for (int h = 0; h < DataColNum; h++)
{
xlSheet.Cells.NumberFormat = "@";
xlSheet.Cells[j + 2, h + 1] = dt.Tables[0].Rows[j][h].ToString().Trim();
}
fom.GetType().GetProperty("PbValue").SetValue(fom, j+3, null);
fom.Refresh();
}
fom.Close();
MessageBox.Show(sys.ChineseCstr("資料匯出成功" ),sys.ChineseCstr("提示" ),MessageBoxButtons.OK, MessageBoxIcon.Information);
//顯示匯出的Excel
excel.Visible = true;
以下为我汇出EXCEL的代码:汇出的很慢,还有其它更好的办法或者是优化
//初始化Excel組件
Excel.Application excel = new Excel.Application();
Excel.Workbook workbookData = excel.Application.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.Worksheet xlSheet = (Excel.Worksheet)workbookData.Worksheets[1];
//設定Excel標題
for (int i = 0; i < DataColNum; i++)
{
//獲取列名
string ColName = dt.Tables[0].Columns[i].ColumnName.ToString().Trim();
xlSheet.Cells[1, i + 1] = sys.ChineseCstr(ColName);
}
fom.GetType().GetProperty("PbValue").SetValue(fom, 2, null);
fom.Refresh();
//設定Excel資料區
for (int j = 0; j < DataRowNum; j++)
{
for (int h = 0; h < DataColNum; h++)
{
xlSheet.Cells.NumberFormat = "@";
xlSheet.Cells[j + 2, h + 1] = dt.Tables[0].Rows[j][h].ToString().Trim();
}
fom.GetType().GetProperty("PbValue").SetValue(fom, j+3, null);
fom.Refresh();
}
fom.Close();
MessageBox.Show(sys.ChineseCstr("資料匯出成功" ),sys.ChineseCstr("提示" ),MessageBoxButtons.OK, MessageBoxIcon.Information);
//顯示匯出的Excel
excel.Visible = true;
{
SaveFileDialog saveFile = new SaveFileDialog();
saveFile.Filter = "Excel files(*.xls)|*.xls";
saveFile.FilterIndex = 0;
saveFile.RestoreDirectory = true;
saveFile.CreatePrompt = true;
saveFile.Title = "导出Excel文件到";
DateTime now = DateTime.Now;
saveFile.FileName = now.ToShortDateString();
saveFile.ShowDialog(); Stream myStream;
myStream = saveFile.OpenFile();
StreamWriter sw = new StreamWriter(myStream, Encoding.GetEncoding("gb2312"));
string str = " ";
//写标题
for(int i = 0 ;i<dv.Table.Columns.Count;i++)
{
if(i>0)
{
str += "\t";
}
str += dv.Table.Columns[i].ColumnName;
}
sw.Write(str);
//写内容
for(int rowNo =0;rowNo<dv.Count;rowNo++)
{
string tempstr = " ";
for(int columnNo =0;columnNo<dv.Table.Columns.Count;columnNo++)
{
if(columnNo>0)
{
tempstr += "\t";
}
tempstr += dv.Table.Rows[rowNo][columnNo].ToString();
}
sw.WriteLine(tempstr);
}
sw.Close();
myStream.Close();
} private void simpleButton1_Click(object sender, EventArgs e)
{
DataView dataView = new DataView();
dataView.Table = _dtHistoryPoints;
ExportExcel(dataView);
}
菜鸟路过,我用的网上找的这个方法,没感觉到慢