为方便下次遇到不知道去哪找先把它存放在这里,以下是保存导出word主要类方法
public class BiultReportForm
{
/// <summary>word 应用对象 </summary>
private Microsoft.Office.Interop.Word.Application _wordApplication; /// <summary>word 文件对象 </summary>
private Microsoft.Office.Interop.Word.Document _wordDocument; /// <summary>
/// 创建word应用对象
/// </summary>
public void CreateAWord()
{ //实例化word应用对象
this._wordApplication = new Microsoft.Office.Interop.Word.Application(); Object myNothing = System.Reflection.Missing.Value;
this._wordDocument = this._wordApplication.Documents.Add(ref myNothing, ref myNothing, ref myNothing, ref myNothing);
} /// <summary>
/// 创建word应用对象
/// </summary>
/// <param name="strPath">文件目录</param>
public void CreateAWord(string strPath)
{
//判断是否存在目录没有就创建
if (!Directory.Exists(strPath))
{
Directory.CreateDirectory(strPath);
}
//实例化word应用对象
this._wordApplication = new Microsoft.Office.Interop.Word.Application();
Object myNothing = System.Reflection.Missing.Value;
this._wordDocument = this._wordApplication.Documents.Add(ref myNothing, ref myNothing, ref myNothing, ref myNothing);
} /// <summary>
/// 添加页眉
/// </summary>
/// <param name="pPageHeader">标题内容</param>
public void SetPageHeader(string pPageHeader)
{
//添加页眉
this._wordApplication.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdOutlineView;
this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader;
this._wordApplication.ActiveWindow.ActivePane.Selection.InsertAfter(pPageHeader);
//设置中间对齐
this._wordApplication.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
//跳出页眉设置
this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;
} /// <summary>
/// 插入文字
/// </summary>
/// <param name="pText">文本信息</param>
/// <param name="pFontSize">字体打小</param>
/// <param name="pFontColor">字体颜色</param>
/// <param name="pFontBold">字体粗体</param>
/// <param name="ptextAlignment">方向</param>
public void InsertText(string pText, int pFontSize, Microsoft.Office.Interop.Word.WdColor pFontColor, int pFontBold, Microsoft.Office.Interop.Word.WdParagraphAlignment ptextAlignment)
{
//设置字体样式以及方向
this._wordApplication.Application.Selection.Font.Size = pFontSize;
this._wordApplication.Application.Selection.Font.Bold = pFontBold;
this._wordApplication.Application.Selection.Font.Color = pFontColor;
this._wordApplication.Application.Selection.ParagraphFormat.Alignment = ptextAlignment;
this._wordApplication.Application.Selection.TypeText(pText);
} /// <summary>
/// 换行
/// </summary>
public void NewLine()
{
//换行
this._wordApplication.Application.Selection.TypeParagraph();
}
/// <summary>
/// 插入一个图片
/// </summary>
/// <param name="pPictureFileName">图片名称</param>
public void InsertPicture(string pPictureFileName)
{
object myNothing = System.Reflection.Missing.Value;
//图片居中显示
this._wordApplication.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
this._wordApplication.Application.Selection.InlineShapes.AddPicture(pPictureFileName, ref myNothing, ref myNothing, ref myNothing);
} /// <summary>
/// 保存文件
/// </summary>
/// <param name="pFileName">传入路径和保存的文件名称</param>
public void SaveWord(string pFileName)
{
object myNothing = System.Reflection.Missing.Value;
object myFileName = pFileName;
object myWordFormatDocument = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;
object myLockd = false;
object myPassword = "";
object myAddto = true;
try
{
this._wordDocument.SaveAs(ref myFileName, ref myWordFormatDocument, ref myLockd, ref myPassword, ref myAddto, ref myPassword,
ref myLockd, ref myLockd, ref myLockd, ref myLockd, ref myNothing, ref myNothing, ref myNothing,
ref myNothing, ref myNothing, ref myNothing); }
catch
{
throw new Exception("导出word文档失败!");
}
} /// <summary>
/// 创建目录
/// </summary>
/// <param name="_directory">目录</param>
/// <param name="strPath">地址</param>
private void CreatrDirectory(string _directory, string strPath)
{
//判断是否存在目录没有就创建
if (!Directory.Exists(_directory))
{
Directory.CreateDirectory(_directory);
}
//...
}
/// <summary>
/// 获得当前绝对路径
/// </summary>
/// <param name="strPath">指定的路径</param>
/// <returns>绝对路径</returns>
public string GetMapPath(string strPath)
{
if (strPath.ToLower().StartsWith("http://"))
{
return strPath;
}
if (HttpContext.Current != null)
{
return HttpContext.Current.Server.MapPath(strPath);
}
else //非web程序引用
{
strPath = strPath.Replace("/", "\\");
if (strPath.StartsWith("\\"))
{
strPath = strPath.Substring(strPath.IndexOf('\\', )).TrimStart('\\');
}
return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
}
} }
前台调用部分
先保证当前文档名称不重复
//时间+随机数
private string chkCodeRequest()
{ string chkCode = string.Empty; //随机的字符集
char[] character = { '', '', '', '', '', '', '', 'a', 'b', 'd', 'e', 'f', 'h', 'k', 'm', 'n', 'r', 'x', 'y', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };
Random rnd = new Random();
//字符串
for (int i = ; i < ; i++)
{
chkCode += character[rnd.Next(character.Length)];
}
chkCode += DateTime.Now.ToString("yyyyMMddHHmmssffff");
return chkCode ;
}
保存部分
//写入word与保存
private void SavWord()
{
BiultReportForm word = new BiultReportForm();
string patc = @"f:\测试文件名称\";//目录
word.CreateAWord(patc);//可以带目录参数也可为空
word.SetPageHeader("测试页眉");//设置页面 如果没有就不调用
string str = "dsws";
for (int i = ; i < ; i++)
{
str += i.ToString();
word.InsertText(str, , Microsoft.Office.Interop.Word.WdColor.wdColorBlue, i, Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter);//插入内容
word.NewLine();//换行
} patc += "测试文件名称";
patc += chkCodeRequest();
word.SaveWord(patc);
}
或者
SaveFileDialog save = new SaveFileDialog(); //过滤器
save.Filter = "*.doc|*.doc|(*.*)|*.*"; //显示
if (save.ShowDialog() == DialogResult.OK)
{
string name = save.FileName;
// FileInfo info = new FileInfo(name);
//info.Create();
BiultReportForm word = new BiultReportForm();
word.CreateAWord();
word.SetPageHeader("测试页眉");
string str = "dsws";
for (int i = ; i < ; i++)
{
str += i.ToString();
word.InsertText(str, , Microsoft.Office.Interop.Word.WdColor.wdColorBlue, i, Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter);//插入内容
word.NewLine();//换行
}
word.SaveWord(name); }