C#如何向word里面写入数据,求详细代码,感谢大神

凶猛的大白菜 2012-10-13 04:37:19
如题
...全文
270 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
拥抱开源 2013-03-18
  • 打赏
  • 举报
回复
Eric_2014SH 2012-10-13
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Data;
using System.IO;
using NPOI.SS.UserModel;
using NPOI.HSSF.UserModel;
using NPOI.SS.Util;
using System.Text;
using System.Reflection;

public class OfficeHelper : HSSFWorkbook
{
public OfficeHelper()
: base()
{
}
public OfficeHelper(Stream stream) : base(stream) { }

public void CreateCells(int sheetIndex, int rowIndex, int fromCellIndex, int toCellIndex)
{
IRow row = this.GetSheetAt(sheetIndex).GetRow(rowIndex);
for (int i = fromCellIndex; i <= toCellIndex; i++)
row.CreateCell(i);
}

public void Meger(int sheetIndex, int fromRow, int toRow, int fromCol, int toCol)
{
this.GetSheetAt(sheetIndex)
.AddMergedRegion(new CellRangeAddress(fromRow, toRow, fromCol, toCol));
}

public void FillDataToSheet(int sheetIndex, DataTable dt)
{
IRow row = this.GetSheetAt(sheetIndex).CreateRow(0);
for (int i = 0; i < dt.Columns.Count; i++)
row.CreateCell(i)
.SetCellValue(dt.Columns[i].ColumnName);
for (int i = 0; i < dt.Rows.Count; i++)
{
int rowIndex = this.GetSheetAt(sheetIndex).LastRowNum + 1;
row = this.GetSheetAt(sheetIndex).CreateRow(rowIndex);
for (int j = 0; j < dt.Columns.Count; j++)
row.CreateCell(j).SetCellValue(dt.Rows[i][j].ToString());
}
}

public void FillDataToSheet<TEntity>(int sheetIndex, IList<TEntity> list, Dictionary<string, string> fieldAndNames)
{
DataTable dt = GetTable(list, fieldAndNames);
FillDataToSheet(sheetIndex, dt);
}

public void FillDataToSheet<TEntity>(string sheetName, IList<TEntity> list, Dictionary<string, string> fieldAndNames)
{
DataTable dt = GetTable(list, fieldAndNames);
FillDataToSheet(sheetName, dt);
}

public void FillDataToSheet(string sheetName, DataTable dt)
{
ISheet sheet = this.GetSheet(sheetName);
IRow row = sheet.CreateRow(0);
for (int i = 0; i < dt.Columns.Count; i++)
row.CreateCell(i)
.SetCellValue(dt.Columns[i].ColumnName);
for (int i = 0; i < dt.Rows.Count; i++)
{
int rowIndex = sheet.LastRowNum + 1;
row = sheet.CreateRow(rowIndex);
for (int j = 0; j < dt.Columns.Count; j++)
{
row.CreateCell(j).SetCellValue(dt.Rows[i][j].ToString());
}
}
}

public byte[] GetContentBytes()
{
if (this.NumberOfSheets == 0)
this.CreateSheet("sheet1");
MemoryStream stream = new MemoryStream();
this.Write(stream);
byte[] result = stream.ToArray();
stream.Close();
this.Dispose();
return result;
}

private DataTable GetTable<TEntity>(IList<TEntity> list, Dictionary<string, string> keyVals)
{
string index = "序号";
DataTable dt = new DataTable();
PropertyInfo[] propertys = typeof(TEntity).GetProperties(
BindingFlags.Instance | BindingFlags.Public);
dt.Columns.Add(index);
foreach (KeyValuePair<string, string> keyVal in keyVals)
dt.Columns.Add(keyVal.Value);

for (int i = 0; i < list.Count; i++)
{
StringBuilder sb = new StringBuilder();
IList<object> rowVals = new List<object>();
rowVals.Add(i + 1);
for (int j = 0; j < keyVals.Keys.Count; j++)
{
PropertyInfo property = propertys
.Where(p => p.Name == keyVals.Keys.ToArray()[j])
.SingleOrDefault();
object val = property.GetValue(
list[i], property.GetIndexParameters());
rowVals.Add(val);
}
dt.Rows.Add(rowVals.ToArray());
}
return dt;
}

}
Eric_2014SH 2012-10-13
  • 打赏
  • 举报
回复
你可以用npoi,他可以写入word,excel,而且实现office支持的各种格式和样式
不知道你要的word是否需要设置颜色,格式,你可以网上搜下
凶猛的大白菜 2012-10-13
  • 打赏
  • 举报
回复
那个dateset是什么呀,我就是把一些字符串写入word,该怎么弄,谢谢啦
l331326473 2012-10-13
  • 打赏
  • 举报
回复
http://www.cnblogs.com/lshguang89/archive/2008/05/23/1205922.html
网上例子很多,希望对你有帮助。
http://blog.csdn.net/jinjiangting/article/details/5151514
l331326473 2012-10-13
  • 打赏
  • 举报
回复
public bool ExportWord(DataSet ds, string saveFileName)
{
bool fileSaved = false;
object filename = saveFileName;
try
{
Object Nothing = System.Reflection.Missing.Value;
//创建Word文档
Word.Application WordApp = new Word.ApplicationClass();
Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);

//文档中创建表格
WordApp.Selection.TypeParagraph();
Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 13, 5, ref Nothing, ref Nothing);
//设置表格样式
//newTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleThickThinLargeGap;
//newTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
newTable.Columns[1].Width = 100f;
newTable.Columns[2].Width = 100f;
newTable.Columns[3].Width = 100f;
newTable.Columns[4].Width = 50f;
newTable.Columns[5].Width = 50f;

//填充表格内容
newTable.Cell(1, 1).Range.Text = "登记表";
newTable.Cell(1, 1).Range.Bold = 3;//设置单元格中字体为粗体

//合并单元格
newTable.Cell(1, 1).Merge(newTable.Cell(1, 5));
WordApp.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中

object missing = System.Reflection.Missing.Value;
object unit;
unit = Word.WdUnits.wdStory;
WordApp.Selection.EndKey(ref unit, ref missing);
WordApp.Selection.TypeParagraph();
//文件保存
WordDoc.SaveAs(ref 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);
fileSaved = true;
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
if (WordApp != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(WordApp);
WordApp = null;
}
GC.Collect();

}
catch
{
fileSaved = false;
}
return fileSaved;
}

110,825

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧