没做过excel 不知道如何做..
Application app = new Application();
app.Visible= false;
app.UserControl = true;
Workbooks wkbs = app.Workbooks;
_Workbook wkb = wkbs.Add(XlWBATemplate.xlWBATWorksheet);
Sheets shee = wkb.Worksheets;
_Worksheet wshee = (_Worksheet) shee.get_Item(1);
wshee.Cells[1 , 1]="1";
wkb.SaveAs("d:\\1.xls" , Missing.Value ,
Missing.Value , Missing.Value , Missing.Value , Missing.Value ,
XlSaveAsAccessMode.xlNoChange ,
Missing.Value , Missing.Value ,
Missing.Value , Missing.Value , Missing.Value);这是找的 2.0版本的....无法合并列..不知道列样式是什么...请高人指点.最好附上一个demo
谢谢...
Application app = new Application();
app.Visible= false;
app.UserControl = true;
Workbooks wkbs = app.Workbooks;
_Workbook wkb = wkbs.Add(XlWBATemplate.xlWBATWorksheet);
Sheets shee = wkb.Worksheets;
_Worksheet wshee = (_Worksheet) shee.get_Item(1);
wshee.Cells[1 , 1]="1";
wkb.SaveAs("d:\\1.xls" , Missing.Value ,
Missing.Value , Missing.Value , Missing.Value , Missing.Value ,
XlSaveAsAccessMode.xlNoChange ,
Missing.Value , Missing.Value ,
Missing.Value , Missing.Value , Missing.Value);这是找的 2.0版本的....无法合并列..不知道列样式是什么...请高人指点.最好附上一个demo
谢谢...
{
object miss = Missing.Value;
Application excelApp = new Application();
excelApp.Workbooks.Add(miss);
excelApp.DisplayAlerts = false;
System.IO.FileInfo aopath = new System.IO.FileInfo(path);
aopath.CopyTo(path1, true); Worksheet workSheet = (Worksheet)excelApp.Worksheets[1];
int rowCount = dataTable.Rows.Count;
int colCount = dataTable.Columns.Count;
object[,] dataArray = new object[rowCount + 1, colCount];
int colIndex = 0;
foreach (DataColumn col in dataTable.Columns)
{ dataArray[0, colIndex] = col.ColumnName;
colIndex++;
} for (int i = 1; i < rowCount + 1; i++)
{
for (int j = 0; j < colCount; j++)
{
dataArray[i, j] = dataTable.Rows[i - 1][j].ToString();
}
}
workSheet.get_Range(workSheet.Cells[1, 1], workSheet.Cells[rowCount + 1, colCount]).Value2 = dataArray;
workSheet.get_Range(workSheet.Cells[1, 1], workSheet.Cells[rowCount + 1, colCount]).HorizontalAlignment = XlVAlign.xlVAlignCenter;
workSheet.get_Range(workSheet.Cells[1, 1], workSheet.Cells[rowCount + 1, colCount]).Columns.AutoFit(); Workbook workBook = excelApp.Workbooks[1];
workBook.RefreshAll();
if (System.IO.File.Exists(path1))
{
System.IO.File.Delete(path1);
} workBook.SaveAs(path1, miss, miss, miss, miss, miss,
XlSaveAsAccessMode.xlNoChange, miss, miss, miss, miss, miss); workBook.Close(false, miss, miss);
excelApp.Workbooks.Close();
excelApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
if (dataTable != null)
{
foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcessesByName("Excel"))
{
if (p.CloseMainWindow() == false)
{
p.Kill();
}
}
}
workBook = null;
excelApp = null;
GC.Collect();
if (System.IO.File.Exists(path1))
{
return true;
}
else
{
return false;
}
}
{
object miss = Missing.Value;
Application excelApp = new Application();
excelApp.Workbooks.Add(miss);
excelApp.DisplayAlerts = false;
System.IO.FileInfo aopath = new System.IO.FileInfo(path);
aopath.CopyTo(path1, true); Worksheet workSheet = (Worksheet)excelApp.Worksheets[1];
int rowCount = dataTable.Rows.Count;
int colCount = dataTable.Columns.Count;
object[,] dataArray = new object[rowCount + 1, colCount];
int colIndex = 0;
foreach (DataColumn col in dataTable.Columns)
{ dataArray[0, colIndex] = col.ColumnName;
colIndex++;
} for (int i = 1; i < rowCount + 1; i++)
{
for (int j = 0; j < colCount; j++)
{
dataArray[i, j] = dataTable.Rows[i - 1][j].ToString();
}
}
workSheet.get_Range(workSheet.Cells[1, 1], workSheet.Cells[rowCount + 1, colCount]).Value2 = dataArray;
workSheet.get_Range(workSheet.Cells[1, 1], workSheet.Cells[rowCount + 1, colCount]).HorizontalAlignment = XlVAlign.xlVAlignCenter;
workSheet.get_Range(workSheet.Cells[1, 1], workSheet.Cells[rowCount + 1, colCount]).Columns.AutoFit(); Workbook workBook = excelApp.Workbooks[1];
workBook.RefreshAll();
if (System.IO.File.Exists(path1))
{
System.IO.File.Delete(path1);
} workBook.SaveAs(path1, miss, miss, miss, miss, miss,
XlSaveAsAccessMode.xlNoChange, miss, miss, miss, miss, miss); workBook.Close(false, miss, miss);
excelApp.Workbooks.Close();
excelApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
if (dataTable != null)
{
foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcessesByName("Excel"))
{
if (p.CloseMainWindow() == false)
{
p.Kill();
}
}
}
workBook = null;
excelApp = null;
GC.Collect();
if (System.IO.File.Exists(path1))
{
return true;
}
else
{
return false;
}
}
excel 样式如何做啊