就是 我在textbox里填的一些数据 如何把它自动填写到我原本就准备好的excel里编程思维是这样(这三步我都不会.. 希望高手能详细指点):
1.我事先准备一份已经有表格的excel
2.在程序界面里textbox这样的控件里所填的数据填写至excel某个行某个列 (也就是所对应的ABCDEF... 和12345...)
3.然后保存或打印(要在程序界面里操作,也就是我界面弄个按钮写打印,保存,另存为 等等按钮都能够对应到excel相应的操作)
1.我事先准备一份已经有表格的excel
2.在程序界面里textbox这样的控件里所填的数据填写至excel某个行某个列 (也就是所对应的ABCDEF... 和12345...)
3.然后保存或打印(要在程序界面里操作,也就是我界面弄个按钮写打印,保存,另存为 等等按钮都能够对应到excel相应的操作)
把datagridview的数据输出为Excel,Word的简单应用
Posted by 上来下去
public static void ExportData(DataGridView srcDgv,string fileName)//导出数据,传入一个datagridview和一个文件路径
{
string type = fileName.Substring(fileName.IndexOf(”.”)+1);//获得数据类型
if (type.Equals(”xls”,StringComparison.CurrentCultureIgnoreCase))//Excel文档
{
Excel.Application excel = new Excel.Application();
try
{
excel.DisplayAlerts = false;
excel.Workbooks.Add(true);
excel.Visible = false;for (int i = 0; i < srcDgv.Columns.Count; i++)//设置标题
{
excel.Cells[2, i+1] = srcDgv.Columns[i].HeaderText;
}for (int i = 0; i < srcDgv.Rows.Count; i++)//填充数据
{
for (int j = 0; j < srcDgv.Columns.Count; j++)
{
excel.Cells[i + 3, j + 1] = srcDgv[j, i].Value;
}
}excel.Workbooks[1].SaveCopyAs(fileName);//保存
}
finally
{
excel.Quit();
}
return;
}//保存Word文件if (type.Equals(”doc”, StringComparison.CurrentCultureIgnoreCase))
{object path = fileName;Object none=System.Reflection.Missing.Value;
Word.Application wordApp = new Word.Application();
Word.Document document = wordApp.Documents.Add(ref none, ref none, ref none, ref none);
//建立表格
Word.Table table= document.Tables.Add(document.Paragraphs.Last.Range, srcDgv.Rows.Count+1, srcDgv.Columns.Count, ref none, ref none);try
{for (int i = 0; i < srcDgv.Columns.Count; i++)//设置标题
{
table.Cell(1, i + 1).Range.Text = srcDgv.Columns[i].HeaderText;
}for (int i = 0; i < srcDgv.Rows.Count; i++)//填充数据
{
for (int j = 0; j < srcDgv.Columns.Count; j++)
{table.Cell(i + 2, j + 1).Range.Text = srcDgv[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);}
finally
{
wordApp.Quit(ref none, ref none, ref none);
}}}
这里就可以指定到某个行某个列
string filename = @"E:\tongji.xls";
Excel.Application objApp = new Excel.Application();
Excel.Workbooks workbooks = objApp.Workbooks;
object missing = Missing.Value;
workbooks.Open(filename, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing);
Excel.Sheets objSheets = objApp.Worksheets;
Excel._Worksheet objSheet;
//采用索引的方式规定写入Index张的工作表,在指定位置插入数据
objSheet = (Excel._Worksheet)objSheets.get_Item(1);
objSheet.Cells[3, 3] = "'1407000403512";
objSheet.Cells[4, 3] = "aaaaaaaaa";
objSheet.Cells[5, 3] = "bbbbbbbbb";
objSheet.Cells[6, 3] = "ccccccccc";
objSheet.Cells[7, 3] = "dddddddddd"; //去掉保存的对话框,若肯定无重复的文件或需要提示,将下面两行注释即可
objApp.DisplayAlerts = false;
objSheet.SaveAs(filename, missing, missing, missing, missing, missing, missing, missing, missing, missing);
objApp.Workbooks.Close();
//退出依然会驻留进程,因此进行垃圾回收
objApp.Quit();
System.GC.Collect();
从数据库取数据按格式生成几千张excel报表
大概花了一个星期的时间 思路如下 先做一个excel模板 里面要填写东西的地方填上标记 如 张三 15 男 等等
生成的时候 先复制这个文件,然后一个字段一个字段的替换再保存就可以了
请不要忘记加分。
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using System.Reflection;
using System.Collections;
using Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;namespace ExcelLib {
public class ExcelUtility {
/// <summary>
/// 读取指定单元格的内容,注意,下标已经转换为从0开始xxxxx
/// </summary>
/// <param name="sheet"></param>
/// <param name="rowIndex">行的序号</param>
/// <param name="colIndex"></param>
/// <returns></returns>
public static string ReadCellText(Worksheet sheet, int rowIndex, int colIndex) {
Range range1 = (Range)sheet.Cells[rowIndex + 1, colIndex + 1];
string s = (string)range1.Text;
Marshal.ReleaseComObject(range1); return s;
}
/// <summary>
/// 写入指定单元格的内容,注意,下标已经转换为从0开始 /// </summary>
/// <param name="sheet"></param>
/// <param name="rowIndex"></param>
/// <param name="colIndex"></param>
/// <param name="text"></param>
public static void WriteCell(Worksheet sheet, int rowIndex, int colIndex, string text) {
sheet.Cells[rowIndex + 1, colIndex + 1] = text;
//ReadCellText(null,
}
public static void InsertImage(Worksheet sheet, int rowIndex, int colIndex, string filename) {
Range r = (Range)sheet.Cells[rowIndex + 1, colIndex + 1];
sheet.Activate();
r.Select();
Pictures pics = (Pictures)sheet.Pictures(Type.Missing);
pics.Insert(filename, Type.Missing);
}
public static Sheets FindSheets(Application app) {
Workbooks books = app.Workbooks;
Workbook book = books[1];
Sheets sheets = book.Worksheets;
Marshal.ReleaseComObject(book);
Marshal.ReleaseComObject(books);
return sheets;
}
public static Worksheet FindSheet(Sheets sheets, string name) {
foreach (Worksheet sheet in sheets) {
if (sheet.Name == name) {
return sheet;
}
}
return null;
}
public static Application OpenExcel(string fileName) {
Application app = new ApplicationClass();
app.Workbooks.Open(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
return app;
} public static int GetCellMergeCount(Worksheet sheet, int rowIndex, int colIndex) {
Range range1 = (Range)sheet.Cells[rowIndex + 1, colIndex + 1];
Range rangeUnderThis = range1.get_Offset(1, 0); int count = rangeUnderThis.Row - range1.Row;
Marshal.ReleaseComObject(rangeUnderThis);
Marshal.ReleaseComObject(range1); return count;
}
private static string GetColumnList(PropertyInfo[] properties) {
StringBuilder sb = new StringBuilder();
foreach (PropertyInfo prop in properties) {
sb.Append(prop.Name);
sb.Append(",");
}
sb.Remove(sb.Length - 1, 1);
return sb.ToString();
} private static string[] GetObjectPropertyValues(PropertyInfo[] properties, object obj) {
string[] result = new string[properties.Length];
for (int i = 0; i < properties.Length; i++) {
object propertyObj = properties[i].GetGetMethod().Invoke(obj, null);
result[i] = propertyObj == null ? "" : propertyObj.ToString();
}
return result;
} private static void CreateSheet(PropertyInfo[] properties, string sheetname, OleDbConnection con) {
StringBuilder sb = new StringBuilder();
foreach (PropertyInfo prop in properties) {
sb.Append(prop.Name);
sb.Append(" string,");
}
sb.Remove(sb.Length - 1, 1);
OleDbCommand cmd = con.CreateCommand();
cmd.CommandText = string.Format("CREATE TABLE {0} ({1})", sheetname, sb);
cmd.ExecuteNonQuery();
} private static PropertyInfo[] GetProperties(Type itemType) {
return itemType.GetProperties(BindingFlags.Instance | BindingFlags.Public); } private static Type GetItemType1(object obj) {
if (!(obj is IEnumerable)) {
throw new InvalidOperationException("need a IEnumerable<T>");
}
Type[] types = obj.GetType().FindInterfaces(new TypeFilter(ExcelUtility_filter), "IEnumerable");
foreach (Type t in types) {//t是IEnumerable<T>或者IEnumerable
if (t.IsGenericType) {//t是IEnumerable<T>
MethodInfo m = t.GetMethod("GetEnumerator");
Type ienumeratorType = m.ReturnType;
return ienumeratorType.GetGenericArguments()[0];
}
}
throw new InvalidOperationException("need a IEnumerable<T>"); } static bool ExcelUtility_filter(Type m, object filterCriteria) {
if (m.Name.StartsWith("IEnumerable")) {
if (m.IsGenericType) {
return true;
}
}
return false;
} }
}