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

c#通过datatable导出excel和word

2014-08-15 09:24 411 查看
/// <summary>
/// 导出datatable到word
/// </summary>
/// <param name="dg">需要中文列名的表</param>
public static void ExportWord(DataTable dg)
{
try
{
if (dg.Rows.Count != 0)
{
//建表格
object Nothing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
object start = 0;
object end = 0;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, dg.Rows.Count+1, dg.Columns.Count, ref Nothing, ref Nothing);
Word.Table newTable = oDoc.Tables[1];
newTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
newTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
object beforeRow = newTable.Rows[1];
newTable.Rows.Add(ref beforeRow);

//赋值
for(int i=0;i<dg.Columns.Count;i++)
{
newTable.Cell(1, i+1).Range.Text = dg.Columns[i].ColumnName;
}
for (int i = 0; i < dg.Rows.Count; i++)
{
for (int j = 0; j < dg.Columns.Count; j++)
{
newTable.Cell(i + 2, j+1).Range.Text = dg.Rows[i][j].ToString();
}
}

object fileName = "";
oDoc.SaveAs(fileName, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
oWord.Documents.Open(fileName, Nothing, false, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing);
}
}
catch
{ }

/// <summary>
/// 导出datatable到Excel
/// </summary>
/// <param name="dg">需要中文列名的表</param>
public static void ExportExcel(DataTable dg)
{
try
{
if (dg.Rows.Count != 0)
{
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
//Microsoft.Office.Interop.Excel.ApplicationClass ac = new Microsoft.Office.Interop.Excel.ApplicationClass();

Microsoft.Office.Interop.Excel.Workbook wb; //这里千万不能使用 new 来实例对象,不然会异常

Microsoft.Office.Interop.Excel.Worksheet ws;

wb = app.Workbooks.Add(System.Reflection.Missing.Value);  //创建工作簿(WorkBook:即Excel文件主体本身)
ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.Add(System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value);        //创建工作表(Worksheet:工作表,即Excel里的子表sheet)

//设置表名
ws.Name = dg.TableName == "" ? "Table" : dg.TableName;
//赋值
for (int i = 0; i < dg.Columns.Count; i++)
{
ws.Cells[1, i + 1] = dg.Columns[i].ColumnName;
}
//将数据导入到工作表的单元格
for (int i = 0; i < dg.Rows.Count; i++)
{
for (int j = 0; j < dg.Columns.Count; j++)
ws.Cells[i + 2, j + 1] = dg.Rows[i][j].ToString();
}
string strPath = Environment.CurrentDirectory;
Microsoft.Win32.SaveFileDialog dialogOpenFile = new Microsoft.Win32.SaveFileDialog();
dialogOpenFile.DefaultExt = "xls";//默认扩展名
dialogOpenFile.AddExtension = true;//是否自动添加扩展名
dialogOpenFile.Filter = "*.xls|.xls";
dialogOpenFile.OverwritePrompt = true;//文件已存在是否提示覆盖
dialogOpenFile.FileName = ws.Name;//默认文件名
dialogOpenFile.CheckPathExists = true;//提示输入的文件名无效
dialogOpenFile.Title = "保存EXCEL";
//显示对话框
bool? b = dialogOpenFile.ShowDialog();
if (b == true)//点击保存
{
//保存到文件
wb.SaveAs(dialogOpenFile.FileName, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
}

//恢复系统路径-涉及不到的可以去掉
Environment.CurrentDirectory = strPath;

//关闭
wb.Close(System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value);

}
}
catch
{ }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: