您的位置:首页 > 编程语言 > C#

C# 导出数据到Excel

2010-11-22 21:22 323 查看
public static void ExportExcel(DataSet tempds, string saveFileName)
{
if (tempds == null)
{
MessageBox.Show("要导出的数据为空!!!","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
else
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
MessageBox.Show("无法创建Excel对象,可能您的电脑未安装Excel","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
try
{
if (xlApp == null)
{
MessageBox.Show("无法创建Excel对象,可能您的电脑未安装Excel", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
xlApp.Visible = false;
bool fileSaved = false;
Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
Microsoft.Office.Interop.Excel.Range range;
long totalCount = tempds.Tables[0].Rows.Count;
long rowRead = 0;
float percent = 0;
for (int i = 0; i < tempds.Tables[0].Columns.Count; i++)
{
worksheet.Cells[1, i + 1] = tempds.Tables[0].Columns[i].ColumnName;
//range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[2, i + 1];
//range.Interior.ColorIndex = 15;
//range.Font.Bold = true;
}
//写入数值
int[] stringFlags = new int[60];
for (int r = 0; r < tempds.Tables[0].Rows.Count; r++)
{
for (int i = 0; i < tempds.Tables[0].Columns.Count; i++)//判断有多少是字符类型
{
if (r == 0)
{
string type = tempds.Tables[0].Columns[i].DataType.ToString();
if (type == "System.String")
{
stringFlags[i] = 1;
range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[r + 2, i + 1];
range.NumberFormatLocal = "@";
}
worksheet.Cells[r + 2, i + 1] = tempds.Tables[0].Rows[r][i];

}
else
{
if (stringFlags[i] == 1)//对于字符类型的处理
{
range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[r + 2, i + 1];
range.NumberFormatLocal = "@";
}
worksheet.Cells[r + 2, i + 1] = tempds.Tables[0].Rows[r][i];

}
}
rowRead++;
percent = ((float)(100 * rowRead)) / totalCount;
//lbInfo.Text = "正在导出数据[" + percent.ToString("0.00") + "%]...";
//Microsoft.Office.Interop.Excel.Application.DoEvents();
}
if (saveFileName != "")
{
try
{
workbook.Saved = true;
workbook.SaveCopyAs(saveFileName);
fileSaved = true;
}
catch (Exception ex)
{
fileSaved = false;
MessageBox.Show("导出文件时出错,文件可能正被打开!","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
}
else
{
fileSaved = false;
}
}
catch (Exception ee)
{
MessageBox.Show("导出发生错误,错误信息为:" + ee.Message.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
finally
{
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
xlApp = null;
}
}
}

public static void ExportDataGridViewToExcel(DataGridViewX MyDataGridView)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "导出Excel文件到";

if (saveFileDialog.ShowDialog() != DialogResult.Cancel)
{
Stream myStream;
myStream = saveFileDialog.OpenFile();

StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));
string str = "";
try
{
//写标题
for (int i = 0; i < MyDataGridView.ColumnCount; i++)
{
if (MyDataGridView.Columns[i].Visible == true)
{
if (i > 0)
{
str += "/t";
}
str += MyDataGridView.Columns[i].HeaderText;
}
}

sw.WriteLine(str);
//写内容
for (int j = 0; j < MyDataGridView.Rows.Count; j++)
{
string tempStr = "";

for (int k = 0; k < MyDataGridView.Columns.Count; k++)
{
if (MyDataGridView.Columns[k].Visible == true)
{
if (k > 0)
{
tempStr += "/t";
}
if (MyDataGridView.Rows[j].Cells[k].Value != null)
{
tempStr += MyDataGridView.Rows[j].Cells[k].Value.ToString();
}
}
}
sw.WriteLine(tempStr);
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
finally
{
sw.Close();
myStream.Close();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: