//结算单的DATAGRID导出EXECL中
public void OutPutExcel(DataGridView dg, string lei, string pamtime, string printtime)
{
Excel.Application excel;
Excel.Workbook xBk;
Excel.Worksheet xSt;
excel = new Excel.ApplicationClass();
xBk = excel.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
excel.Application.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);
excel.Application.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);
xSt = (Excel.Worksheet)xBk.Sheets[1];
//第一个sheet
xSt.Name = "111";
xSt.Cells[1, 1] = "11";
//第二个sheet
xSt = (Excel.Worksheet)xBk.Sheets[2];
xSt.Name = "222";
xSt.Cells[1, 1] = "222"; //第三个sheet
xSt = (Excel.Worksheet)xBk.Sheets[3];
xSt.Name = "3333";
xSt.Cells[1, 1] = "3333";
excel.Visible = true; //EXECL可见
excel.Cells.WrapText = true;
}
public void OutPutExcel(DataGridView dg, string lei, string pamtime, string printtime)
{
Excel.Application excel;
Excel.Workbook xBk;
Excel.Worksheet xSt;
excel = new Excel.ApplicationClass();
xBk = excel.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
excel.Application.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);
excel.Application.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);
xSt = (Excel.Worksheet)xBk.Sheets[1];
//第一个sheet
xSt.Name = "111";
xSt.Cells[1, 1] = "11";
//第二个sheet
xSt = (Excel.Worksheet)xBk.Sheets[2];
xSt.Name = "222";
xSt.Cells[1, 1] = "222"; //第三个sheet
xSt = (Excel.Worksheet)xBk.Sheets[3];
xSt.Name = "3333";
xSt.Cells[1, 1] = "3333";
excel.Visible = true; //EXECL可见
excel.Cells.WrapText = true;
}
解决方案 »
- 全局热键的问题
- 公司自定义了office的模板,要求开发一个office加载项的程序,自动加载模板
- 关于textbox怎样才能只能输入数字
- 急!!!画表格
- DisPage这个控件哪里有呀?
- 如何给类写说明
- 程序员三生三世的郁闷之一(我倒,调试了三天了,不知错在那儿)
- C#中有类似于ACDSEE软件的图象处理的控件?
- 带时区的日期格式如何绑定DataGridView
- 面对char* (long *time,char *str1)的C++方法该定义成怎样的C#方法
- 怎么保存DataGridView(显示的表是用代码写出来的)里的内容(经过修改,删除)到数据库中???
- 给定任何两个句子,如何把第二句中和第一句中相同的单词给删除掉?
int state = 0;
int tablecount = ds.Tables.Count;
if (tablecount <= 0) return 0;
int index = 0;
Excel.Application xlApp = null;
Excel.Workbook xlBook = null;
Excel.Worksheet xlSheet = null;
int rowIndex = 1;
int colIndex = 0;
object missing = Missing.Value; List<System.Data.DataTable> listTable = new List<System.Data.DataTable>();
try
{
xlApp = new Excel.Application(); xlBook = xlApp.Workbooks.Add(true); foreach (System.Data.DataTable dt in ds.Tables)
{
index++;
rowIndex = 1;
colIndex = 0; xlSheet = (Excel.Worksheet)xlApp.Worksheets[1]; xlSheet.Name = dt.TableName; foreach (DataColumn Col in dt.Columns)
{
colIndex = colIndex + 1;
xlApp.Cells[1, colIndex] = " " + Col.ColumnName;
}
int rowNum = dt.Rows.Count;
int colNum = dt.Columns.Count;
string[,] finalData = new string[rowNum, colNum];
for (int i = 0; i < rowNum; i++)
{
for (int j = 0; j < colNum; j++)
{
finalData[i, j] = dt.Rows[i][j].ToString();
}
}
xlSheet.get_Range(xlSheet.Cells[1, 1], xlSheet.Cells[1, colIndex]).Font.Bold = true;
xlSheet.get_Range(xlSheet.Cells[1, 1], xlSheet.Cells[rowIndex, colIndex]).Borders.LineStyle = 1;
xlSheet.get_Range(xlSheet.Cells[2, 1], xlSheet.Cells[rowNum + 1, colNum]).Value2 = finalData;
xlSheet.get_Range(xlSheet.Cells[2, 1], xlSheet.Cells[rowNum + 1, colNum]).NumberFormatLocal = "@"; if (index < tablecount) xlSheet = (Excel.Worksheet)xlApp.Worksheets.Add(missing, missing, missing, missing);
} if (System.IO.File.Exists(filename)) System.IO.File.Delete(filename);
xlApp.Visible = false;
xlSheet.SaveAs(filename, missing, missing, missing, missing, missing, missing, missing, missing, missing);
state = 1;
}
finally
{
object saveChange = true;
xlBook.Close(saveChange, filename, missing);
xlApp.Quit();
if(xlSheet!=null) Marshal.ReleaseComObject(xlSheet);
if (xlBook != null) Marshal.ReleaseComObject(xlBook);
if (xlApp != null) Marshal.ReleaseComObject(xlApp);
xlApp = null;
xlBook = null;
xlSheet = null;
GC.Collect();
}
return state;