/**//// <summary>
/// 数据集转换 即把DataSet转换为Word对象
/// </summary>
/// <param name="ds"></param>
public bool DataSetToWord( DataSet ds, string strFileName )
{
bool result = false;
if ( ds == null ) return result;
if ( strFileName == null || strFileName == "" ) return result;
if ( this.Open() )
{
try
{
Microsoft.Office.Interop.Word.Range para = oWordApplic.Application.Selection.Paragraphs.Item(1).Range;
object tableBehavior = System.Reflection.Missing.Value;
object autoFitBehavior = System.Reflection.Missing.Value;
oDoc.Tables.Add(para,
ds.Tables[0].Rows.Count + 1, //多的一行用来表示表列
ds.Tables[0].Columns.Count,
ref tableBehavior,
ref autoFitBehavior); //填充Word表格的列标
for (int intCol = 0; intCol < ds.Tables[0].Columns.Count; intCol++)
{
oDoc.Tables.Item(1).Cell(1, intCol + 1).Range.InsertBefore(ds.Tables[0].Columns[intCol].ColumnName.Trim());
} //填充Word表格的内容
for (int intRow = 0; intRow < ds.Tables[0].Rows.Count; intRow++)
{
for (int intCol = 0; intCol < ds.Tables[0].Columns.Count; intCol++)
{
oDoc.Tables.Item(1).Cell(intRow + 2, intCol + 1).Range.InsertBefore(ds.Tables[0].Rows[intRow][intCol].ToString().Trim());
}
} //Microsoft.Office.Interop.Word.Range para = oWordApplic.Application.Selection.Paragraphs.First .Range;
//object tableBehavior = System.Reflection.Missing.Value;
//object autoFitBehavior = System.Reflection.Missing.Value;
//oDoc.Tables.Add(para,
// ds.Tables[0].Rows.Count + 1, //多的一行用来表示表列
// ds.Tables[0].Columns.Count,
// ref tableBehavior,
// ref autoFitBehavior);
//////填充Word表格的列标
//for (int intCol = 0; intCol < ds.Tables[0].Columns.Count; intCol++)
//{
// //oDoc.Tables.Item(1).Cell(1, intCol + 1).Range.InsertBefore(ds.Tables[0].Columns[intCol].ColumnName.Trim());
// oDoc.Tables[0].Cell(1, intCol + 1).Range.InsertBefore(ds.Tables[0].Columns[intCol].ColumnName.Trim());
//}
//////填充Word表格的内容
//for (int intRow = 0; intRow < ds.Tables[0].Rows.Count; intRow++)
//{
// for (int intCol = 0; intCol < ds.Tables[0].Columns.Count; intCol++)
// {
// oDoc.Tables[0].Cell(intRow + 2, intCol + 1).Range.InsertBefore(ds.Tables[0].Rows[intRow][intCol].ToString().Trim());
// }
//} //保存
if ( this.SaveAs( strFileName ) )
result = true;
}
catch(Exception x)
{
System.Diagnostics.Trace.WriteLine(x);
this.Quit();
//throw ( new Exception() );
}
}
return result;
}
oWordApplic.Application.Selection.Paragraphs.Item(1).Range;
错误:
“Microsoft.Office.Interop.Word.Paragraphs”不包含“Item”的定义,并且找不到可接受类型为“Microsoft.Office.Interop.Word.Paragraphs”的第一个参数的扩展方法“Item”(是否缺少 using 指令或程序集引用?)
为什么没这方法,怎么替代
之前引用的源代码出去
第一步:先手工创建一个Word文档,先设定好格式
在需要填充数据的地方,插入书签第二步:程序调用时,使用File.Copy()以上面创建的模板复制一个新的文档,
从数据库中读数据,并填充到指定的(书签)位置代码参考:
using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;/// <summary>
/// 文件导出类
/// </summary>
public class Export
{
/// <summary>
/// 导出简历
/// </summary>
/// <param name="rid">简历ID</param>
/// <returns></returns>
public static bool ExportResume(int rid)
{
Resume resume = new Resume();
if (!resume.Exist(rid)) return false; //查询数据
DataSet dataResume = resume.QueryResumeById(rid);
CompositeCategory category = new CompositeCategory();
DataSet item = new DataSet();
DataRow currRow = dataResume.Tables[0].Rows[0];
resume.Id = rid;
resume.JobSkillExperience = currRow["jobSkillExperience"].ToString();
resume.Mid = Convert.ToInt32(currRow["mid"]);
string curr_pay = currRow["pay"].ToString();
item = category.QueryCategoryById(curr_pay);
resume.Pay = item.Tables[0].Rows[0]["cName"].ToString();
resume.TrainingExperience = currRow["trainingExperience"].ToString();
string curr_workAge = currRow["workAge"].ToString();
item = category.QueryCategoryById(curr_workAge);
resume.WorkAge = item.Tables[0].Rows[0]["cName"].ToString();
resume.WorkExperience = currRow["workExperience"].ToString();
string curr_industry = currRow["workIndustry"].ToString();
item = category.QueryCategoryById(curr_industry);
resume.WorkIndustry = item.Tables[0].Rows[0]["cName"].ToString();
string curr_job = currRow["workJob"].ToString();
item = category.QueryCategoryById(curr_job);
resume.WorkJob = item.Tables[0].Rows[0]["cName"].ToString();
string curr_place = currRow["workPlace"].ToString();
item = category.QueryCategoryById(curr_place);
resume.WorkPlace = item.Tables[0].Rows[0]["cName"].ToString();
string curr_workType = currRow["workType"].ToString();
item = category.QueryCategoryById(curr_workType);
resume.WorkType = item.Tables[0].Rows[0]["cName"].ToString();
PersonalInfo pinfo = new PersonalInfo();
DataSet dataPInfo = pinfo.QueryPersonalInfoByUid(resume.Mid);
currRow = dataPInfo.Tables[0].Rows[0];
pinfo.Address = currRow["address"].ToString();
pinfo.Birthday = Convert.ToDateTime(currRow["birthday"].ToString());
string curr_census = currRow["census"].ToString();
item = category.QueryCategoryById(curr_census);
pinfo.Census = item.Tables[0].Rows[0]["cName"].ToString();
string curr_education = currRow["education"].ToString();
item = category.QueryCategoryById(curr_education);
pinfo.Education = item.Tables[0].Rows[0]["cName"].ToString();
pinfo.IdCard = currRow["idCard"].ToString();
pinfo.Phone = currRow["phone"].ToString();
pinfo.RealName = currRow["realName"].ToString();
pinfo.Re = currRow["re"].ToString();
pinfo.Sex = Convert.ToInt32(currRow["sex"]); Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application(); System.Web.HttpServerUtility server = System.Web.HttpContext.Current.Server;
//server.MapPath("WordTemplat/ResumeTemplate.doc"
string templateFileName = server.MapPath("~/WordTemplat/ResumeTemplate.doc"); //"..\WordTemplatResumeTemplate.doc";
Member member = new Member();
DataSet dataMember = member.QueryMemberById(resume.Mid);
member.Uid = dataMember.Tables[0].Rows[0]["uid"].ToString();
string fileName = server.MapPath("~/WordTemplat/TempDoc/" + member.Uid + "-个人简历.doc");
//复制模板
if (File.Exists(fileName)) File.Delete(fileName);
File.Copy(templateFileName, fileName);
Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document(); object Obj_FileName = fileName;
object Visible = false;
object ReadOnly = false;
object missing = System.Reflection.Missing.Value;
//打开文件
doc = app.Documents.Open(ref Obj_FileName, ref missing, ref ReadOnly, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref Visible,
ref missing, ref missing, ref missing,
ref missing);
doc.Activate(); //写入数据
object what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBook;
//1。姓名
object realName = "realName";
doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref realName);
doc.ActiveWindow.Selection.TypeText(pinfo.RealName);
//doc.ActiveWindow.Selection.TypeParagraph();换行 //2。性别
object sex = "sex";
doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref sex);
if(pinfo.Sex==1) doc.ActiveWindow.Selection.TypeText("男");
else doc.ActiveWindow.Selection.TypeText("女"); //3。年龄
object age = "age";
doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref age);
int curr_age = DateTime.Now.Year - pinfo.Birthday.Year;
doc.ActiveWindow.Selection.TypeText(curr_age.ToString());
//...
object IsSave = true;
doc.Close(ref IsSave, ref missing, ref missing);
app.Quit(ref missing, ref missing, ref missing); System.IO.FileInfo fileInfo = new System.IO.FileInfo(fileName);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(Encoding.UTF8.GetBytes(fileInfo.Name)));
HttpContext.Current.Response.AddHeader("content-length", fileInfo.Length.ToString());
HttpContext.Current.Response.ContentType = "application/vnd.ms-word";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
HttpContext.Current.Response.WriteFile(fileName); //File.Delete(fileName); return true;
} public static bool DeleteTempDoc(int rid)
{
return true;
}
}希望能帮到你!
通过书签添加Table
在添加列
我还是没想到较好的解决方法