我正在使用扩展方法将DataTable导出到Excel。当我格式化单元格样式时,它将对齐和字体大小应用于工作表中的所有单元格。
这是我的代码
public static void ExportToExcel(this System.Data.DataTable DataTable, string ExcelFilePath = null, string address = "", string reportName = "")
{
try
{
int ColumnsCount;
if (DataTable == null || (ColumnsCount = DataTable.Columns.Count) == 0)
throw new Exception("ExportToExcel: Null or empty input table!\n");
// load excel, and create a new workbook
Microsoft.Office.Interop.Excel.Application Excel = new Microsoft.Office.Interop.Excel.Application();
Excel.Workbooks.Add();
// single worksheet
Microsoft.Office.Interop.Excel._Worksheet Worksheet = Excel.ActiveSheet;
object[] Header = new object[ColumnsCount];
// column headings
for (int i = 0; i < ColumnsCount; i++)
Header[i] = DataTable.Columns[i].ColumnName;
Microsoft.Office.Interop.Excel.Range HeaderRange = Worksheet.get_Range((Microsoft.Office.Interop.Excel.Range)(Worksheet.Cells[10, 1]), (Microsoft.Office.Interop.Excel.Range)(Worksheet.Cells[10, ColumnsCount]));
HeaderRange.Value = Header;
HeaderRange.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Gold);
HeaderRange.Font.Bold = true;
// DataCells
int RowsCount = DataTable.Rows.Count;
object[,] Cells = new object[RowsCount, ColumnsCount];
for (int j = 0; j < RowsCount; j++)
for (int i = 0; i < ColumnsCount; i++)
Cells[j, i] = DataTable.Rows[j][i];
//Custom Header
Worksheet.Range[Worksheet.Cells[1, 1], Worksheet.Cells[1, ColumnsCount]].Merge();
Worksheet.Range[Worksheet.Cells[1, 1], Worksheet.Cells[2, ColumnsCount]].Merge();
Worksheet.Cells[1, 1].Value = "DISTRIBUTOR SYSTEM";
Worksheet.Cells[1, 1].Style.Font.Size = 18;
Worksheet.Cells[1, 1].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Aqua);
Worksheet.Cells[1].Style.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
Worksheet.Cells[1].Style.VerticalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
Worksheet.Range[Worksheet.Cells[3, 1], Worksheet.Cells[3, 2]].Merge();
Worksheet.Cells[3, 1].Value = "Invoice Date";
Worksheet.Cells[3, 1].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Aqua);
Worksheet.Range[Worksheet.Cells[3, 3], Worksheet.Cells[3, 4]].Merge();
Worksheet.Cells[3, 3].Value = System.DateTime.Today.ToShortDateString();
Worksheet.Cells[3, 3].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Aqua);
Worksheet.Range[Worksheet.Cells[4, 1], Worksheet.Cells[4, ColumnsCount]].Merge();
Worksheet.Cells[4, 1].Value = reportName;
Worksheet.Cells[4, 1].Style.Font.Size = 18;
Worksheet.Range[Worksheet.Cells[6, 1], Worksheet.Cells[6, ColumnsCount]].Merge();
Worksheet.Cells[6, 1].Value = address;
//Worksheet.Range[Worksheet.Cells[1, 1], Worksheet.Cells[1, 7]].Style.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
//Worksheet.Range[Worksheet.Cells[1, 1], Worksheet.Cells[1, 7]].Style.VerticalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
//Worksheet.Range[Worksheet.Cells[11, 7], Worksheet.Cells[RowsCount + 1, 7]].Style.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignRight;
Worksheet.get_Range((Microsoft.Office.Interop.Excel.Range)(Worksheet.Cells[11, 1]), (Microsoft.Office.Interop.Excel.Range)(Worksheet.Cells[RowsCount + 1, ColumnsCount])).Value = Cells;
Worksheet.Range[Worksheet.Cells[10, 4], Worksheet.Cells[RowsCount + 1, 4]].EntireColumn.NumberFormat = "yyyy/MM/dd hh:mm";
Worksheet.Range[Worksheet.Cells[10, 11], Worksheet.Cells[RowsCount + 1, 7]].EntireColumn.NumberFormat = "####0.00";
Worksheet.Cells[RowsCount + 2, 1].Value = "TOTAL";
Worksheet.Cells[RowsCount + 2, 1].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Gold);
Worksheet.Cells[RowsCount + 2, 7].Formula = "=Sum(" + Worksheet.Cells[11, 7].Address + ":" + Worksheet.Cells[RowsCount + 1, 7].Address + ")";
Worksheet.Columns[7].ColumnWidth = 18.00;
Worksheet.Columns[6].ColumnWidth = 18.00;
Worksheet.Columns[5].ColumnWidth = 18.00;
// check fielpath
if (ExcelFilePath != null && ExcelFilePath != "")
{
try
{
Worksheet.SaveAs(ExcelFilePath);
Excel.Quit();
MessageBox.Show("Excel file saved!");
}
catch (Exception ex)
{
throw new Exception("ExportToExcel: Excel file could not be saved! Check filepath.\n"
+ ex.Message);
}
}
else // no filepath is given
{
Excel.Visible = true;
}
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
throw new Exception("ExportToExcel: \n" + ex.Message);
}
}
我只需要在单元格1,单元格4和单元格6中水平和垂直设置字体大小和对齐中心 帮助PLZ
答案 0 :(得分:1)
在Excel中使用AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
long startTime = HereTimestampThatGenerateBefore;
Intent intentApp = new Intent(getApplicationContext(), TheServiceHere.class);
PendingIntent pendingIntentApp = PendingIntent
.getService(getApplicationContext(), 0, intentApp, PendingIntent.FLAG_ONE_SHOT);
alarmManager.set(AlarmManager.RTC, startTime,
pendingIntentApp);
Interop似乎会更改整个工作表中的样式,如下所示:Changing font size of one cell in excel using C#
尝试将字体和对齐方式更改直接应用于单元格/范围,无需访问其样式。
答案 1 :(得分:0)
要在单元格1,单元格4和单元格6中水平和垂直设置字体大小和对齐中心,请尝试
{
Worksheet.Cells[1, i].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
Worksheet.Cells[1, i].Style.VerticalAlignment = OfficeOpenXml.Style.ExcelVerticalAlignment.Center;
Worksheet.Cells[1, i].Style.Font.Size = 20;
}