C# Stream流方式导入Excel,htm,txt,Doc
来源:互联网 发布:网络剧推荐 编辑:程序博客网 时间:2024/06/05 07:15
/// <summary>
/// 流方式导出Excel
/// </summary>
/// <param name="dgv"></param>
private void ExportToExcel(DataGridView dgv)
{
if (dgv.Rows.Count == 0)
{
MessageBox.Show("没有数据可供导出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
// 保存对话框
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Execl文件 (*.xls)|*.xls|Xml文件 (*.xml)|*.xml|网页文件 (*.htm)|*.htm|网页文件 (*.html)|*.html|Doc文件 (*.doc)|*.doc|文本文件 (*.txt)|*.txt|所有文件 (*.*)|*.*";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "导出文件保存路径";//为Excel
saveFileDialog.FileName = null;
saveFileDialog.ShowDialog();
string Path = saveFileDialog.FileName;
//被点了"取消"
if (Path.IndexOf(":") < 0)
{
return;
}
//获得数据类型
string type = Path.Substring(Path.IndexOf(".") + 1);
if (type.Equals("doc", StringComparison.CurrentCultureIgnoreCase))
{
//object path = Path;
//Object none = System.Reflection.Missing.Value;
//try
//{
// Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
// Microsoft.Office.Interop.Word.Document document = wordApp.Documents.Add(ref none, ref none, ref none, ref none);
// //建立表格
// Microsoft.Office.Interop.Word.Table table = document.Tables.Add(document.Paragraphs.Last.Range, dgv.Rows.Count + 1, dgv.Columns.Count, ref none, ref none);
// try
// {
// for (int i = 0; i < dgv.Columns.Count; i++)//设置标题
// {
// table.Cell(1, i + 1).Range.Text = dgv.Columns[i].HeaderText;
// }
// for (int i = 0; i < dgv.Rows.Count; i++)//填充数据
// {
// for (int j = 0; j < dgv.Columns.Count; j++)
// {
// table.Cell(i + 2, j + 1).Range.Text = dgv[j, i].Value.ToString();
// }
// }
// document.SaveAs(ref path, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none);
// document.Close(ref none, ref none, ref none);
// MessageBox.Show("数据已经成功导出到:" + Path, "导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
// }
// catch (Exception ex)
// {
// MessageBox.Show("文件 " + Path + " 正由另一起程使用,/r/n请先关闭该进程!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
// return;
// }
// finally
// {
// wordApp.Quit(ref none, ref none, ref none);
// }
// return;
//}
//catch (Exception ex)
//{
// MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
// return;
//}
}
Stream myStream;
try
{
myStream = saveFileDialog.OpenFile();
}
catch (Exception ex)
{
MessageBox.Show("文件 " + Path + " 正由另一起程使用,/r/n请先关闭该进程!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
string columnTitle = "";
if (type.Equals("htm", StringComparison.CurrentCultureIgnoreCase) || type.Equals("html", StringComparison.CurrentCultureIgnoreCase))
{
//写入列标题
columnTitle += "<table align=/"center/" border=/"1px/"><tr>";
for (int i = 0; i < dgv.ColumnCount; i++)
{
columnTitle += "<td>";
columnTitle += dgv.Columns[i].HeaderText + "</td>";
}
//写入列内容
for (int j = 0; j < dgv.Rows.Count; j++)
{
columnTitle += "</tr><tr>";
for (int k = 0; k < dgv.Columns.Count; k++)
{
columnTitle += "<td>";
if (dgv.Rows[j].Cells[k].Value == null)
columnTitle += " </td>";
else
columnTitle += dgv.Rows[j].Cells[k].Value.ToString().Trim() + "</td>";
}
}
columnTitle += "</tr></table>";
try
{
sw.WriteLine(columnTitle);
}
catch
{
MessageBox.Show("文件 " + Path + " 正由另一起程使用,/r/n请先关闭该进程!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
finally
{
sw.Close();
myStream.Close();
}
MessageBox.Show("数据已经成功导出到:" + Path, "导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
try
{
//写入列标题
for (int i = 0; i < dgv.ColumnCount; i++)
{
if (i > 0)
{
columnTitle += "/t";
}
columnTitle += dgv.Columns[i].HeaderText;
}
sw.WriteLine(columnTitle);
//写入列内容
for (int j = 0; j < dgv.Rows.Count; j++)
{
string columnValue = "";
for (int k = 0; k < dgv.Columns.Count; k++)
{
if (k > 0)
{
columnValue += "/t";
}
if (dgv.Rows[j].Cells[k].Value == null)
columnValue += "";
else
columnValue += dgv.Rows[j].Cells[k].Value.ToString().Trim();
}
sw.WriteLine(columnValue);
}
sw.Close();
myStream.Close();
MessageBox.Show("数据已经成功导出到:" + Path, "导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
finally
{
sw.Close();
myStream.Close();
}
}
- C# Stream流方式导入Excel,htm,txt,Doc
- C# Stream流方式导入Excel,htm,txt,Doc
- c#操作excel方式一:stream简单读写excel
- c#操作excel方式一:stream简单读写excel
- word转htm,excel转htm, doc转pdf
- C# WinForm 导出导入Excel/Doc 完整实例教程
- Table导出为xls,doc,txt,htm方法
- Table导出为xls,doc,txt,htm方法
- Table导出为xls,doc,txt,htm方法
- excel txt 导入 sql
- excel txt 导入 sql
- txt文件导入excel
- C#把txt的数据导入excel,用逗号分隔
- openmeetings 中 导入 doc ppt txt
- C#读取doc,pdf,ppt,TXT文件
- asp.net c#下htm文件另存为doc文档
- C# winform 导出导入Excel/Doc 完整实例教程 使用Aspose.Cells.dll——第二篇
- C# winform 导出导入Excel/Doc 完整实例教程 使用Aspose.Cells.dll——第一篇
- linux 内核源代码目录结构概述
- 常用的八个cmd网络命令
- C++ 八皇后实现代码
- 在div中实现垂直居中的方法
- asp.net中操作Excel全集[1]
- C# Stream流方式导入Excel,htm,txt,Doc
- 使用 AppFuse 快速构建 J2EE 应用
- 赶鸭子上架
- 条款27: 尽量少做转型操作
- 关于EeePc的Linux下安装GCC编译器
- [转] 【幽默搞笑】笑死你,别怪我!(狠逗、狠强)
- 关于自己(11/26/2009)
- [转] 不差钱后遗症
- [转] 让人笑到晕倒的QQ个人说明