public static bool outWuZi(DataGridView dgv,dt)
{
Excel.ApplicationClass myExcel = new Excel.ApplicationClass();
myExcel.Application.Workbooks.Add(true);
Excel.Workbook wb =(Excel.Workbook) myExcel.Application.Workbooks[1];
Excel.Worksheet sheet = (Excel.Worksheet)wb.Sheets[1];
sheet.Name = "名称表";
myExcel.Caption = "名称表"; //表名
//生成标题
Excel.Range range;
int m = 0;
for (int i = 0; i < dgv.Columns.Count; i++)
{
if (dgv.Columns[i].Visible)
{
sheet.Cells[1, m + 1] = dgv.Columns[i].HeaderText;
m++;
}
}
try
{
for (int i = 0; i < dt.Rows.Count; i++)
{
int mm = 0;
for (int j = 0; j < dt.Columns.Count; j++)
{
sheet.Cells[i + 2, mm + 1] = dt.Rows[i][j].ToString();
mm++;
}
}
range = sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[dt.Rows.Count + 1, dgv.Columns.GetColumnCount(DataGridViewElementStates.Visible)]);
range.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
myExcel.Application.Cells.EntireColumn.AutoFit(); //自动列宽
myExcel.Application.Cells.VerticalAlignment = Excel.Constants.xlCenter;
myExcel.Application.Cells.HorizontalAlignment = Excel.Constants.xlLeft;
range = sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[1, dgv.Columns.GetColumnCount(DataGridViewElementStates.Visible)]);
range.Font.Bold = true;
range.HorizontalAlignment = Excel.Constants.xlCenter;
myExcel.Visible = true;
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\n 请重试", "错误了");
}
return false;
}
public static bool outWrold(string strHead,string strTitle,string strNeiRong)
{
try
{
Microsoft.Office.Interop.Word.Application _wordApplication = new Microsoft.Office.Interop.Word.Application();
Object myNothing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Document _wordDocument = _wordApplication.Documents.Add(ref myNothing, ref myNothing, ref myNothing, ref myNothing);
_wordApplication.Caption = strTitle;
_wordApplication.Visible = true;
//添加页眉
_wordApplication.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdOutlineView;
_wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader;
_wordApplication.ActiveWindow.ActivePane.Selection.InsertAfter(strHead);
_wordApplication.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
_wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;
//设置标题
_wordApplication.Application.Selection.Font.Size = 16;
_wordApplication.Application.Selection.Font.Bold = 10;
_wordApplication.Application.Selection.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorBlack;
_wordApplication.Application.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
_wordApplication.Application.Selection.TypeText(strTitle);
_wordApplication.Application.Selection.TypeParagraph();
_wordApplication.Application.Selection.TypeParagraph();
//设置内容的样式
_wordApplication.Application.Selection.Font.Size = 12;
_wordApplication.Application.Selection.Font.Bold = 0;
_wordApplication.Application.Selection.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorBlack;
_wordApplication.Application.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
_wordApplication.Application.Selection.TypeText(strNeiRong);
return true;
}
catch
{
return true;
}
}